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
|
@c -*-texinfo-*-
@c This is part of the GNU Emacs Lisp Reference Manual.
@c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1998 Free Software Foundation, Inc.
@c See the file elisp.texi for copying conditions.
@setfilename ../info/eval
@node Evaluation, Control Structures, Symbols, Top
@c @chapter Evaluation
@chapter $BI>2A(B
@c @cindex evaluation
@c @cindex interpreter
@c @cindex interpreter
@c @cindex value of expression
@cindex $BI>2A(B
@cindex $B%$%s%?!<%W%j%?(B
@cindex $B<0$NCM(B
@c The @dfn{evaluation} of expressions in Emacs Lisp is performed by the
@c @dfn{Lisp interpreter}---a program that receives a Lisp object as input
@c and computes its @dfn{value as an expression}. How it does this depends
@c on the data type of the object, according to rules described in this
@c chapter. The interpreter runs automatically to evaluate portions of
@c your program, but can also be called explicitly via the Lisp primitive
@c function @code{eval}.
Emacs Lisp$B$K$*$1$k<0$N(B@dfn{$BI>2A(B}$B!J(Bevaluation$B!K$O!"(B
@dfn{Lisp$B%$%s%?!<%W%j%?(B}$B!J(BLisp interpreter$B!K$,9T$$$^$9!#(B
$B$3$l$O!"F~NO$H$7$F(BLisp$B%*%V%8%'%/%H$r<u$1<h$j!"(B
@dfn{$B<0$H$7$F$NCM(B}$B$r7W;;$9$k%W%m%0%i%`$G$9!#(B
$B7W;;J}K!$O!"K\>O$G=R$Y$k5,B'$K=>$C$F%*%V%8%'%/%H$N%G!<%?7?$K0MB8$7$^$9!#(B
$B%$%s%?!<%W%j%?$O!"FI<T$N%W%m%0%i%`$N$"$kItJ,$rI>2A$9$k$?$a$K(B
$B<+F0E*$KF0:n$7$^$9$,!"(BLisp$B4pK\4X?t(B@code{eval}$B$r2p$7$F(B
$B%$%s%?!<%W%j%?$rL@<(E*$K8F$V=P$9$3$H$b$G$-$^$9!#(B
@ifinfo
@menu
* Intro Eval:: Evaluation in the scheme of things.
* Forms:: How various sorts of objects are evaluated.
* Quoting:: Avoiding evaluation (to put constants in the program).
* Eval:: How to invoke the Lisp interpreter explicitly.
@end menu
@node Intro Eval
@c @section Introduction to Evaluation
@section $BI>2A$H$O(B
@c The Lisp interpreter, or evaluator, is the program that computes
@c the value of an expression that is given to it. When a function
@c written in Lisp is called, the evaluator computes the value of the
@c function by evaluating the expressions in the function body. Thus,
@c running any Lisp program really means running the Lisp interpreter.
Lisp$B%$%s%?!<%W%j%?!"$D$^$j!"%(%P%j%e%(!<%?$O!"(B
$BM?$($i$l$?<0$NCM$r7W;;$9$k%W%m%0%i%`$G$9!#(B
Lisp$B$G=q$$$?4X?t$r8F$S=P$9$H!"%(%P%j%e%(!<%?$O!"$=$N4X?tK\BNFb$N(B
$B<0$rI>2A$9$k$3$H$G4X?t$NCM$r7W;;$7$^$9!#(B
$B$7$?$,$C$F!"$I$s$J(BLisp$B%W%m%0%i%`$N<B9T$G$b!"(B
Lisp$B%$%s%?!<%W%j%?$r<B9T$9$k$3$H$r0UL#$7$^$9!#(B
@c How the evaluator handles an object depends primarily on the data
@c type of the object.
$B%(%P%j%e%(!<%?$K$h$k%*%V%8%'%/%H$N07$$J}$O!"(B
$B<g$K%*%V%8%'%/%H$N%G!<%?7?$K0MB8$7$^$9!#(B
@end ifinfo
@c @cindex forms
@c @cindex expression
@cindex $B%U%)!<%`(B
@cindex $B<0(B
@c A Lisp object that is intended for evaluation is called an
@c @dfn{expression} or a @dfn{form}. The fact that expressions are data
@c objects and not merely text is one of the fundamental differences
@c between Lisp-like languages and typical programming languages. Any
@c object can be evaluated, but in practice only numbers, symbols, lists
@c and strings are evaluated very often.
$BI>2A$9$k$3$H$r0U?^$7$?(BLisp$B%*%V%8%'%/%H$r(B
@dfn{$B<0(B}$B!J(Bexpression$B!K$H$+(B@dfn{$B%U%)!<%`(B}$B!J(Bform$B!K$H8F$S$^$9!#(B
$B<0$O%G!<%?%*%V%8%'%/%H$G$"$jC1$J$k%F%-%9%H$G$O$J$$$H$$$&;v<B$O!"(B
Lisp$BMM8@8l$HE57?E*$J%W%m%0%i%`8@8l$H$N4pK\E*$J0c$$$N(B1$B$D$G$9!#(B
$B$I$s$J%*%V%8%'%/%H$G$bI>2A$G$-$^$9$,!"<BMQ>e$O!"(B
$B?t!"%7%s%\%k!"%j%9%H!"J8;zNs$rI>2A$9$k$3$H$,B?$$$N$G$9!#(B
@c It is very common to read a Lisp expression and then evaluate the
@c expression, but reading and evaluation are separate activities, and
@c either can be performed alone. Reading per se does not evaluate
@c anything; it converts the printed representation of a Lisp object to the
@c object itself. It is up to the caller of @code{read} whether this
@c object is a form to be evaluated, or serves some entirely different
@c purpose. @xref{Input Functions}.
Lisp$B<0$rFI$_<h$j$=$N<0$rI>2A$9$k$3$H$O$H$F$b0lHLE*$J$3$H$G$9$,!"(B
$BFI$_<h$j$HI>2A$OJL!9$NF0:n$G$"$j!"$=$l$>$l$rJL!9$K<B9T$9$k$3$H$b$G$-$^$9!#(B
$BFI$_<h$j<+BN$G$O!"$J$K$bI>2A$7$^$;$s!#(B
Lisp$B%*%V%8%'%/%H$NI=<(I=8=$r%*%V%8%'%/%H$=$N$b$N$KJQ49$7$^$9!#(B
$B$3$N%*%V%8%'%/%H$rI>2A$9$Y$-%U%)!<%`$H$9$k$+!"(B
$B$^$C$?$/JL$NL\E*$K;H$&$+$O!"(B@code{read}$B$N8F$S=P$7B&$G7h$^$j$^$9!#(B
@c Do not confuse evaluation with command key interpretation. The
@c editor command loop translates keyboard input into a command (an
@c interactively callable function) using the active keymaps, and then
@c uses @code{call-interactively} to invoke the command. The execution of
@c the command itself involves evaluation if the command is written in
@c Lisp, but that is not a part of command key interpretation itself.
@c @xref{Command Loop}.
$BI>2A$H%3%^%s%I%-!<$N2r<a$r:.F1$7$J$$$G$/$@$5$$!#(B
$B%(%G%#%?%3%^%s%I%k!<%W$O!"M-8z$J%-!<%^%C%W$rMQ$$$F(B
$B%-!<%\!<%IF~NO$r%3%^%s%I!JBPOCE*$K8F$S=P$72DG=$J4X?t!K$KJQ49$7!"(B
@code{call-interactively}$B$r;H$C$F%3%^%s%I$r5/F0$7$^$9!#(B
$B%3%^%s%I$,(BLisp$B$G=q$$$F$"$l$P!"(B
$B%3%^%s%I<+BN$N<B9T$K$OI>2A$,4X$o$C$F$-$^$9$,!"(B
$B$=$N$3$H$O!"%3%^%s%I%-!<$N2r<a<+BN$K$O4^$^$l$F$$$^$;$s!#(B
@c @cindex recursive evaluation
@cindex $B:F5"E*I>2A(B
@c Evaluation is a recursive process. That is, evaluation of a form may
@c call @code{eval} to evaluate parts of the form. For example, evaluation
@c of a function call first evaluates each argument of the function call,
@c and then evaluates each form in the function body. Consider evaluation
@c of the form @code{(car x)}: the subform @code{x} must first be evaluated
@c recursively, so that its value can be passed as an argument to the
@c function @code{car}.
$BI>2A$O:F5"E*$J=hM}$G$9!#(B
$B$D$^$j!"%U%)!<%`$NI>2A$G$O!"(B
@code{eval}$B$r8F$S=P$7$F$=$N%U%)!<%`$N0lItJ,$rI>2A$9$k$3$H$b$"$j$^$9!#(B
$B$?$H$($P!"4X?t8F$S=P$7$NI>2A$K$*$$$F$O!"$^$:!"(B
$B4X?t8F$S=P$7$N3F0z?t$rI>2A$7$F$+$i!"4X?tK\BN$N3F%U%)!<%`$rI>2A$7$^$9!#(B
@code{(car x)}$B$NI>2A$r9M$($F$_$^$7$g$&!#(B
$B$^$::G=i$K(B@code{x}$B$r:F5"E*$KI>2A$9$kI,MW$,$"$j$^$9!#(B
$B$=$NCM$r4X?t(B@code{car}$B$N0z?t$H$7$FEO$;$k$h$&$K$9$k$N$G$9!#(B
@c Evaluation of a function call ultimately calls the function specified
@c in it. @xref{Functions}. The execution of the function may itself work
@c by evaluating the function definition; or the function may be a Lisp
@c primitive implemented in C, or it may be a byte-compiled function
@c (@pxref{Byte Compilation}).
$B4X?t8F$S=P$7$NI>2A$K$*$$$F$O!":G=*E*$K;XDj$7$?4X?t$r8F$S=P$7$^$9!#(B
@xref{Functions}$B!#(B
$B4X?t$N<B9T$=$N$b$N$b!"4X?tDj5A$rI>2A$9$k>l9g$b$"$j$^$9!#(B
$B$"$k$$$O!"4X?t$O(BC$B8@8l$G<BAu$5$l$?(BLisp$B4pK\4X?t$+$b$7$l$^$;$s$7!"(B
$B%P%$%H%3!<%I4X?t$+$b$7$l$^$;$s!J(B@pxref{Byte Compilation}$B!K!#(B
@c @cindex environment
@cindex $B4D6-(B
@c The evaluation of forms takes place in a context called the
@c @dfn{environment}, which consists of the current values and bindings of
@c all Lisp variables.@footnote{This definition of ``environment'' is
@c specifically not intended to include all the data that can affect the
@c result of a program.} Whenever a form refers to a variable without
@c creating a new binding for it, the value of the variable's binding in
@c the current environment is used. @xref{Variables}.
$B%U%)!<%`$NI>2A$O!"(B@dfn{$B4D6-(B}$B!J(Benvironment$B!K$H8F$P$l$kJ8L.$K$*$$$F(B
$B9T$o$l$^$9!#(B
$B4D6-$H$O!"$9$Y$F$N(BLisp$BJQ?t$N8=:_CM$HB+G{$G$9(B
@footnote{$B!X4D6-!Y$N$3$NDj5A$O!"%W%m%0%i%`$N7k2L$K1F6A$9$k(B
$B$9$Y$F$N%G!<%?$r4^$`$3$H$O0U?^$7$F$$$J$$!#(B}$B!#(B
$B%U%)!<%`$,?7$?$JB+G{$r:n$i$:$KJQ?t$r;2>H$9$k>l9g$K$O!"(B
$B8=:_$N4D6-$K$*$1$k$=$NJQ?t$NB+G{$NCM$r;H$$$^$9!#(B
@xref{Variables}$B!#(B
@c @cindex side effect
@cindex $BI{:nMQ(B
@c Evaluation of a form may create new environments for recursive
@c evaluation by binding variables (@pxref{Local Variables}). These
@c environments are temporary and vanish by the time evaluation of the form
@c is complete. The form may also make changes that persist; these changes
@c are called @dfn{side effects}. An example of a form that produces side
@c effects is @code{(setq foo 1)}.
$B%U%)!<%`$rI>2A$9$k$H!"JQ?t!J(B@pxref{Local Variables}$B!K$rB+G{$7$F!"(B
$B:F5"E*I>2A$N$?$a$N?7$?$J4D6-$r:n$k$3$H$,$"$j$^$9!#(B
$B$3$l$i$N4D6-$O0l;~E*$J$b$N$G!"$=$N%U%)!<%`$NI>2A$r40N;$9$k$H(B
$B>C$($F$7$^$$$^$9!#(B
$B%U%)!<%`$O915WE*$JJQ99$r9T$C$F$b$+$^$$$^$;$s!#(B
$B$3$N$h$&$JJQ99$r(B@dfn{$BI{:nMQ(B}$B!J(Bside effects$B!K$H8F$S$^$9!#(B
$BI{:nMQ$r;}$D%U%)!<%`$NNc$O!"(B@code{(setq foo 1)}$B$G$9!#(B
@c The details of what evaluation means for each kind of form are
@c described below (@pxref{Forms}).
$B%U%)!<%`$N3F<oN`$4$H$NI>2A$N0UL#$N>\:Y$O!"(B
$B0J2<$G@bL@$7$^$9!J(B@pxref{Forms}$B!K!#(B
@node Forms
@c @section Kinds of Forms
@section $B%U%)!<%`$N<oN`(B
@c A Lisp object that is intended to be evaluated is called a @dfn{form}.
@c How Emacs evaluates a form depends on its data type. Emacs has three
@c different kinds of form that are evaluated differently: symbols, lists,
@c and ``all other types''. This section describes all three kinds, one by
@c one, starting with the ``all other types'' which are self-evaluating
@c forms.
$BI>2A$9$k$3$H$r0U?^$7$?(BLisp$B%*%V%8%'%/%H$r(B@dfn{$B%U%)!<%`(B}$B!J(Bform$B!K$H8F$S$^$9!#(B
Emacs$B$,$I$N$h$&$K%U%)!<%`$rI>2A$9$k$+$O!"$=$N%G!<%?7?$K0MB8$7$^$9!#(B
Emacs$B$K$O!"I>2AJ}K!$,0[$J$k(B3$B<oN`$N%U%)!<%`$,$"$j$^$9!#(B
$B%7%s%\%k!"%j%9%H!"$*$h$S!"!X$=$NB>$9$Y$F$N7?!Y$G$9!#(B
$BK\@a$G$O!"(B3$B<oN`$9$Y$F$K$D$$$F(B1$B$D(B1$B$D@bL@$7$^$9!#(B
$B$^$:!"<+8JI>2A7?%U%)!<%`$G$"$k!X$=$NB>$9$Y$F$N7?!Y$+$i@bL@$7$^$9!#(B
@menu
* Self-Evaluating Forms:: Forms that evaluate to themselves.
* Symbol Forms:: Symbols evaluate as variables.
* Classifying Lists:: How to distinguish various sorts of list forms.
* Function Indirection:: When a symbol appears as the car of a list,
we find the real function via the symbol.
* Function Forms:: Forms that call functions.
* Macro Forms:: Forms that call macros.
* Special Forms:: ``Special forms'' are idiosyncratic primitives,
most of them extremely important.
* Autoloading:: Functions set up to load files
containing their real definitions.
@end menu
@node Self-Evaluating Forms
@c @subsection Self-Evaluating Forms
@subsection $B<+8JI>2A7?%U%)!<%`(B
@c @cindex vector evaluation
@c @cindex literal evaluation
@c @cindex self-evaluating form
@cindex $B%Y%/%H%k$NI>2A(B
@cindex $BI>2A!"%Y%/%H%k(B
@cindex $B%j%F%i%k$NI>2A(B
@cindex $BI>2A!"%j%F%i%k(B
@cindex $B<+8JI>2A7?%U%)!<%`(B
@c A @dfn{self-evaluating form} is any form that is not a list or symbol.
@c Self-evaluating forms evaluate to themselves: the result of evaluation
@c is the same object that was evaluated. Thus, the number 25 evaluates to
@c 25, and the string @code{"foo"} evaluates to the string @code{"foo"}.
@c Likewise, evaluation of a vector does not cause evaluation of the
@c elements of the vector---it returns the same vector with its contents
@c unchanged.
@dfn{$B<+8JI>2A7?%U%)!<%`(B}$B!J(Bself-evaluating form$B!K$H$O!"(B
$B%j%9%H$G$b%7%s%\%k$G$b$J$$G$0U$N%U%)!<%`$N$3$H$G$9!#(B
$B<+8JI>2A7?%U%)!<%`$O$=$l<+?H$KI>2A$5$l!"(B
$BI>2A7k2L$OI>2A$5$l$k%*%V%8%'%/%H$HF1$8$b$N$G$9!#(B
$B$D$^$j!"?t(B25$B$O(B25$B$HI>2A$5$l!"(B
$BJ8;zNs(B@code{"foo"}$B$OJ8;zNs(B@code{"foo"}$B$HI>2A$5$l$^$9!#(B
$BF1MM$K!"%Y%/%H%k$rI>2A$7$F$b%Y%/%H%k$N8D!9$NMWAG$rI>2A$9$k$3$H$O$"$j$^$;$s!#(B
$B$=$NFbMF$r$^$C$?$/JQ99$9$k$3$H$J$/!"F1$8%Y%/%H%k$rJV$7$^$9!#(B
@example
@group
@c '123 ; @r{A number, shown without evaluation.}
'123 ; @r{$BI>2A$7$F$$$J$$?t(B}
@result{} 123
@end group
@group
@c 123 ; @r{Evaluated as usual---result is the same.}
123 ; @r{$BIaDL$I$*$jI>2A!#7k2L$OF1$8(B}
@result{} 123
@end group
@group
@c (eval '123) ; @r{Evaluated ``by hand''---result is the same.}
(eval '123) ; @r{$B!X<j$G!YI>2A!#7k2L$OF1$8(B}
@result{} 123
@end group
@group
@c (eval (eval '123)) ; @r{Evaluating twice changes nothing.}
(eval (eval '123)) ; @r{2$B2sI>2A$7$F$b$J$K$bJQ$o$i$J$$(B}
@result{} 123
@end group
@end example
@c It is common to write numbers, characters, strings, and even vectors
@c in Lisp code, taking advantage of the fact that they self-evaluate.
@c However, it is quite unusual to do this for types that lack a read
@c syntax, because there's no way to write them textually. It is possible
@c to construct Lisp expressions containing these types by means of a Lisp
@c program. Here is an example:
Lisp$B%3!<%I$K$*$$$F$O!"?t!"J8;z!"J8;zNs!"$5$i$K%Y%/%H%k$G$5$($b!"(B
$B$=$l$i$,<+8JI>2A7?$G$"$k;v<B$rMxMQ$7$F=q$/$N$,IaDL$G$9!#(B
$B$7$+$7!"F~NO9=J8$r;}$?$J$$7?$K$D$$$F$O!"$3$N$h$&$K$7$^$;$s!#(B
$B$H$$$&$N$O!"$=$l$i$r%F%-%9%H$H$7$F=q$/J}K!$,$J$$$+$i$G$9!#(B
$B$=$N$h$&$J7?$r4^$`(BLisp$B<0$r9=@.$9$k$K$O!"(BLisp$B%W%m%0%i%`$r;H$$$^$9!#(B
@example
@group
@c ;; @r{Build an expression containing a buffer object.}
;; @r{$B%P%C%U%!%*%V%8%'%/%H$r4^$`<0$r:n$k(B}
(setq print-exp (list 'print (current-buffer)))
@result{} (print #<buffer eval.texi>)
@end group
@group
@c ;; @r{Evaluate it.}
;; @r{$B$=$l$rI>2A$9$k(B}
(eval print-exp)
@print{} #<buffer eval.texi>
@result{} #<buffer eval.texi>
@end group
@end example
@node Symbol Forms
@c @subsection Symbol Forms
@subsection $B%7%s%\%k%U%)!<%`(B
@c @cindex symbol evaluation
@cindex $B%7%s%\%k$NI>2A(B
@cindex $BI>2A!"%7%s%\%k(B
@c When a symbol is evaluated, it is treated as a variable. The result
@c is the variable's value, if it has one. If it has none (if its value
@c cell is void), an error is signaled. For more information on the use of
@c variables, see @ref{Variables}.
$B%7%s%\%k$rI>2A$9$k$H$-$K$O!"%7%s%\%k$rJQ?t$H$7$F07$$$^$9!#(B
$B$=$N7k2L$O!"CM$,$"$l$P!"JQ?t$NCM$G$9!#(B
$B!JCM%;%k$,6u$G$"$j!KCM$,$J$1$l$P!"%(%i!<$rDLCN$7$^$9!#(B
$BJQ?t$N;H$$J}$K$D$$$F>\$7$/$O!"(B@xref{Variables}$B!#(B
@c In the following example, we set the value of a symbol with
@c @code{setq}. Then we evaluate the symbol, and get back the value that
@c @code{setq} stored.
$B$D$.$NNc$G$O!"(B@code{setq}$B$r;H$C$F%7%s%\%k$NCM$r@_Dj$7$^$9!#(B
$B$=$N$"$H$G%7%s%\%k$rI>2A$9$k$H!"(B@code{setq}$B$GJ]B8$7$?CM$r<h$j=P$;$^$9!#(B
@example
@group
(setq a 123)
@result{} 123
@end group
@group
(eval 'a)
@result{} 123
@end group
@group
a
@result{} 123
@end group
@end example
@c The symbols @code{nil} and @code{t} are treated specially, so that the
@c value of @code{nil} is always @code{nil}, and the value of @code{t} is
@c always @code{t}; you cannot set or bind them to any other values. Thus,
@c these two symbols act like self-evaluating forms, even though
@c @code{eval} treats them like any other symbol. A symbol whose name
@c starts with @samp{:} also self-evaluates in the same way; likewise,
@c its value ordinarily cannot be changed. @xref{Constant Variables}.
$B%7%s%\%k(B@code{nil}$B$H(B@code{t}$B$OFCJL$K07$$!"(B
@code{nil}$B$NCM$O$D$M$K(B@code{nil}$B$G$"$j!"(B
@code{t}$B$NCM$O$D$M$K(B@code{t}$B$G$9!#(B
$B$3$l$i$KJL$NCM$r@_Dj$7$?$j!"JL$NCM$rB+G{$9$k$3$H$O$G$-$^$;$s!#(B
$B$7$?$,$C$F!"(B@code{eval}$B$O$3$l$i$rB>$N%7%s%\%k$HF1MM$K07$$$^$9$,!"(B
$B$3$l$i(B2$B$D$N%7%s%\%k$O<+8JI>2A7?%U%)!<%`$N$h$&$K$U$k$^$$$^$9!#(B
@samp{:}$B$G;O$^$kL>A0$N%7%s%\%k$bF1$80UL#$G<+8JI>2A7?$G$"$j!"(B
$BF1MM$K!"$=$NCM$rJQ99$G$-$^$;$s!#(B
@xref{Constant Variables}$B!#(B
@node Classifying Lists
@c @subsection Classification of List Forms
@subsection $B%j%9%H%U%)!<%`$NJ,N`(B
@c @cindex list form evaluation
@cindex $B%j%9%H%U%)!<%`$NI>2A(B
@cindex $BI>2A!"%j%9%H%U%)!<%`(B
@c A form that is a nonempty list is either a function call, a macro
@c call, or a special form, according to its first element. These three
@c kinds of forms are evaluated in different ways, described below. The
@c remaining list elements constitute the @dfn{arguments} for the function,
@c macro, or special form.
$B%U%)!<%`$,6u$G$O$J$$%j%9%H$J$i$P!"$=$N:G=i$NMWAG$K0MB8$7$F!"(B
$B4X?t8F$S=P$7!"%^%/%m8F$S=P$7!"%9%Z%7%c%k%U%)!<%`$N$$$:$l$+$G$9!#(B
$B$3$l$i$N(B3$B<oN`$N%U%)!<%`$O!"0J2<$K@bL@$9$k$h$&$K!"0[$J$kJ}K!$GI>2A$5$l$^$9!#(B
$B%j%9%H$N;D$j$NMWAG$O!"4X?t!"%^%/%m!"%9%Z%7%c%k%U%)!<%`$N(B
@dfn{$B0z?t(B}$B!J(Barguments$B!K$K$J$j$^$9!#(B
@c The first step in evaluating a nonempty list is to examine its first
@c element. This element alone determines what kind of form the list is
@c and how the rest of the list is to be processed. The first element is
@c @emph{not} evaluated, as it would be in some Lisp dialects such as
@c Scheme.
$B6u$G$O$J$$%j%9%H$rI>2A$9$k:G=i$N<j=g$O!"(B
$B$=$N@hF,MWAG$rD4$Y$k$3$H$G$9!#(B
$B$3$NMWAG$O!"$=$l$@$1$G!"6u$G$O$J$$%j%9%H$N%U%)!<%`$N<oN`$r7hDj$7!"(B
$B%j%9%H$N;D$j$r$I$N$h$&$K=hM}$9$k$+$r7hDj$7$^$9!#(B
Scheme$B$J$I$N(BLisp$B$N0lIt$NJ}8@$H0c$C$F!"@hF,MWAG$OI>2A(B@emph{$B$7$^$;$s(B}$B!#(B
@node Function Indirection
@c @subsection Symbol Function Indirection
@subsection $B%7%s%\%k$N4X?t4V@\(B
@c @cindex symbol function indirection
@c @cindex indirection
@c @cindex void function
@cindex $B%7%s%\%k$N4X?t4V@\(B
@cindex $B4X?t4V@\!"%7%s%\%k(B
@cindex $B4V@\(B
@cindex $B6u4X?t(B
@c If the first element of the list is a symbol then evaluation examines
@c the symbol's function cell, and uses its contents instead of the
@c original symbol. If the contents are another symbol, this process,
@c called @dfn{symbol function indirection}, is repeated until it obtains a
@c non-symbol. @xref{Function Names}, for more information about using a
@c symbol as a name for a function stored in the function cell of the
@c symbol.
$B%j%9%H$N@hF,MWAG$,%7%s%\%k$G$"$k$H!"(B
$BI>2A=hM}$G$O%7%s%\%k$N4X?t%;%k$rD4$Y!"(B
$B$b$H$N%7%s%\%k$N$+$o$j$K$=$NFbMF$r;H$$$^$9!#(B
$B$=$NFbMF$,JL$N%7%s%\%k$G$"$k$H!"(B
@dfn{$B%7%s%\%k$N4X?t4V@\(B}$B!J(Bsymbol function indirection$B!K$H8F$P$l$k(B
$B$3$N=hM}$r%7%s%\%k$G$J$$$b$N$rF@$k$^$G7+$jJV$7$^$9!#(B
$B%7%s%\%k$N4X?t%;%k$K3JG<$5$l$?4X?tL>$H$7$F$N%7%s%\%k$N;H$$J}$K$D$$$F(B
$B>\$7$/$O!"(B@xref{Function Names}$B!#(B
@c One possible consequence of this process is an infinite loop, in the
@c event that a symbol's function cell refers to the same symbol. Or a
@c symbol may have a void function cell, in which case the subroutine
@c @code{symbol-function} signals a @code{void-function} error. But if
@c neither of these things happens, we eventually obtain a non-symbol,
@c which ought to be a function or other suitable object.
$B$3$N=hM}$N7k2L!"L58B%k!<%W$K$J$k>l9g$b$"$j$^$9!#(B
$B$D$^$j!"%7%s%\%k$N4X?t%;%k$,F1$8%7%s%\%k$r;X$7$F$$$k>l9g$G$9!#(B
$B$"$k$$$O!"%7%s%\%k$N4X?t%;%k$,6u$N>l9g$b$"$j$($^$9!#(B
$B$=$N>l9g!"%5%V%k!<%F%#%s(B@code{symbol-function}$B$O!"(B
$B%(%i!<(B@code{void-function}$B$rDLCN$7$^$9!#(B
$B$$$:$l$N>l9g$G$b$J$1$l$P!":G=*E*$K$O%7%s%\%k$G$J$$$b$N$r<hF@$7!"(B
$B$=$l$O4X?t$J$I$NE,@Z$J%*%V%8%'%/%H$G$"$k$O$:$G$9!#(B
@kindex invalid-function
@c @cindex invalid function
@cindex $BIT@5$J4X?t(B
@c More precisely, we should now have a Lisp function (a lambda
@c expression), a byte-code function, a primitive function, a Lisp macro, a
@c special form, or an autoload object. Each of these types is a case
@c described in one of the following sections. If the object is not one of
@c these types, the error @code{invalid-function} is signaled.
$B$h$j@53N$K$$$($P!"(BLisp$B4X?t!J%i%`%@<0!K!"%P%$%H%3!<%I4X?t!"(B
$B4pK\4X?t!"(BLisp$B%^%/%m!"%9%Z%7%c%k%U%)!<%`!"<+F0%m!<%I%*%V%8%'%/%H$N(B
$B$$$:$l$+$r<hF@$7$F$$$k$O$:$G$9!#(B
$B$3$l$i$N3F<oN`$4$H$K!"0J2<$N(B1$B$D(B1$B$D$N@a$G@bL@$7$^$9!#(B
$B%*%V%8%'%/%H$,$3$l$i$N$$$:$l$N7?$G$b$J$$>l9g$K$O!"(B
$B%(%i!<(B@code{invalid-function}$B$rDLCN$7$^$9!#(B
@c The following example illustrates the symbol indirection process. We
@c use @code{fset} to set the function cell of a symbol and
@c @code{symbol-function} to get the function cell contents
@c (@pxref{Function Cells}). Specifically, we store the symbol @code{car}
@c into the function cell of @code{first}, and the symbol @code{first} into
@c the function cell of @code{erste}.
$B$D$.$NNc$O!"%7%s%\%k$N4X?t4V@\$N=hM}$r?^<($7$?$b$N$G$9!#(B
@code{fset}$B$r;H$C$F%7%s%\%k$N4X?t%;%k$K@_Dj$7!"(B
@code{symbol-function}$B$r;H$C$F4X?t%;%k$NFbMF$r<h$j=P$7$^$9(B
$B!J(B@pxref{Function Cells}$B!K!#(B
$B6qBNE*$K$O!"%7%s%\%k(B@code{car}$B$r(B@code{first}$B$N4X?t%;%k$K3JG<$7!"(B
$B%7%s%\%k(B@code{first}$B$r(B@code{erste}$B$N4X?t%;%k$K3JG<$7$^$9!#(B
@smallexample
@group
@c ;; @r{Build this function cell linkage:}
;; @r{$B$3$N$h$&$J4X?t%;%k$N%j%s%/$r:n$k(B}
;; ------------- ----- ------- -------
;; | #<subr car> | <-- | car | <-- | first | <-- | erste |
;; ------------- ----- ------- -------
@end group
@end smallexample
@smallexample
@group
(symbol-function 'car)
@result{} #<subr car>
@end group
@group
(fset 'first 'car)
@result{} car
@end group
@group
(fset 'erste 'first)
@result{} first
@end group
@group
@c (erste '(1 2 3)) ; @r{Call the function referenced by @code{erste}.}
(erste '(1 2 3)) ; @r{@code{erste}$B$,;X$94X?t$r8F$S=P$9(B}
@result{} 1
@end group
@end smallexample
@c By contrast, the following example calls a function without any symbol
@c function indirection, because the first element is an anonymous Lisp
@c function, not a symbol.
$B0lJ}!"$D$.$NNc$G$O!"%7%s%\%k$N4X?t4V@\$r;H$o$:$K4X?t$r8F$S=P$7$^$9!#(B
$B$H$$$&$N$O!"@hF,0z?t$O(BLisp$B$NL5L>4X?t$G$"$C$F!"(B
$B%7%s%\%k$G$O$J$$$+$i$G$9!#(B
@smallexample
@group
((lambda (arg) (erste arg))
'(1 2 3))
@result{} 1
@end group
@end smallexample
@noindent
@c Executing the function itself evaluates its body; this does involve
@c symbol function indirection when calling @code{erste}.
$B4X?t$r<B9T$9$k$3$H$O!"$=$NK\BN$rI>2A$9$k$3$H$G$9!#(B
$B$3$N2aDx$G$O!"(B@code{erste}$B$r8F$S=P$9$H$-$K%7%s%\%k$N4X?t4V@\$,4X$o$j$^$9!#(B
@c The built-in function @code{indirect-function} provides an easy way to
@c perform symbol function indirection explicitly.
$BAH$_9~$_4X?t(B@code{indirect-function}$B$O!"(B
$BL@<(E*$K%7%s%\%k$N4X?t4V@\$r9T$&4JC1$JJ}K!$G$9!#(B
@c Emacs 19 feature
@defun indirect-function function
@c This function returns the meaning of @var{function} as a function. If
@c @var{function} is a symbol, then it finds @var{function}'s function
@c definition and starts over with that value. If @var{function} is not a
@c symbol, then it returns @var{function} itself.
$B$3$N4X?t$O!"4X?t$H$7$F$N(B@var{function}$B$N0UL#$rJV$9!#(B
@var{function}$B$,%7%s%\%k$G$"$l$P(B@var{function}$B$N4X?tDj5A$rC5$7!"(B
$B$=$NCM$+$i:FEY7+$jJV$9!#(B
@var{function}$B$,%7%s%\%k$G$J$1$l$P(B@var{function}$B$=$N$b$N$rJV$9!#(B
@c Here is how you could define @code{indirect-function} in Lisp:
Lisp$B$G(B@code{indirect-function}$B$rDj5A$9$k$H$D$.$N$h$&$K$J$k!#(B
@smallexample
(defun indirect-function (function)
(if (symbolp function)
(indirect-function (symbol-function function))
function))
@end smallexample
@end defun
@node Function Forms
@c @subsection Evaluation of Function Forms
@subsection $B4X?t%U%)!<%`$NI>2A(B
@c @cindex function form evaluation
@c @cindex function call
@cindex $B4X?t%U%)!<%`$NI>2A(B
@cindex $BI>2A!"4X?t%U%)!<%`(B
@cindex $B4X?t8F$S=P$7(B
@c If the first element of a list being evaluated is a Lisp function
@c object, byte-code object or primitive function object, then that list is
@c a @dfn{function call}. For example, here is a call to the function
@c @code{+}:
$BI>2A$9$Y$-%j%9%H$N@hF,MWAG$,!"(BLisp$B4X?t%*%V%8%'%/%H!"(B
$B%P%$%H%3!<%I%*%V%8%'%/%H!"4pK\4X?t%*%V%8%'%/%H$N>l9g!"(B
$B$=$N%j%9%H$O(B@dfn{$B4X?t8F$S=P$7(B}$B!J(Bfunction call$B!K$G$9!#(B
$B$?$H$($P!"$D$.$O!"4X?t(B@code{+}$B$N8F$S=P$7$G$9!#(B
@example
(+ 1 x)
@end example
@c The first step in evaluating a function call is to evaluate the
@c remaining elements of the list from left to right. The results are the
@c actual argument values, one value for each list element. The next step
@c is to call the function with this list of arguments, effectively using
@c the function @code{apply} (@pxref{Calling Functions}). If the function
@c is written in Lisp, the arguments are used to bind the argument
@c variables of the function (@pxref{Lambda Expressions}); then the forms
@c in the function body are evaluated in order, and the value of the last
@c body form becomes the value of the function call.
$B4X?t8F$S=P$7$rI>2A$9$k:G=i$N<j=g$O!"(B
$B%j%9%H$N;D$j$NMWAG$r:8$+$i1&$X=g$KI>2A$9$k$3$H$G$9!#(B
$B$=$N7k2L$O<B0z?t$NCM$K$J$j!"(B1$B$D$NCM$,%j%9%H$N(B1$B$D$NMWAG$KBP1~$7$^$9!#(B
$B$D$.$N<j=g$O!"0z?t$N%j%9%H$G4X?t$r8F$S=P$9$3$H$G!"(B
$B<B<AE*$K$O!"4X?t(B@code{apply}$B!J(B@pxref{Calling Functions}$B!K$r;H$$$^$9!#(B
$B4X?t$,(BLisp$B$G=q$$$F$"$l$P!"4X?t$N0z?tJQ?t$rB+G{$9$k$?$a$K0z?t$r;H$$$^$9(B
$B!J(B@pxref{Lambda Expressions}$B!K!#(B
$B$=$7$F!"4X?tK\BN$N%U%)!<%`72$r=gHV$KI>2A$7!"(B
$BK\BN$N:G8e$N%U%)!<%`$NCM$,4X?t8F$S=P$7$NCM$K$J$j$^$9!#(B
@node Macro Forms
@c @subsection Lisp Macro Evaluation
@subsection Lisp$B%^%/%m$NI>2A(B
@c @cindex macro call evaluation
@cindex $B%^%/%m8F$S=P$7$NI>2A(B
@cindex $BI>2A!"%^%/%m8F$S=P$7(B
@c If the first element of a list being evaluated is a macro object, then
@c the list is a @dfn{macro call}. When a macro call is evaluated, the
@c elements of the rest of the list are @emph{not} initially evaluated.
@c Instead, these elements themselves are used as the arguments of the
@c macro. The macro definition computes a replacement form, called the
@c @dfn{expansion} of the macro, to be evaluated in place of the original
@c form. The expansion may be any sort of form: a self-evaluating
@c constant, a symbol, or a list. If the expansion is itself a macro call,
@c this process of expansion repeats until some other sort of form results.
$BI>2A$9$Y$-%j%9%H$N@hF,MWAG$,%^%/%m%*%V%8%'%/%H$N>l9g!"(B
$B$=$N%j%9%H$O(B@dfn{$B%^%/%m8F$S=P$7(B}$B!J(Bmacro call$B!K$G$9!#(B
$B%^%/%m8F$S=P$7$rI>2A$9$k$H$-$O!"%j%9%H$N;D$j$NMWAG$rI>2A(B@emph{$B$7$^$;$s(B}$B!#(B
$B$=$N$+$o$j$K!"MWAG$=$N$b$N$r%^%/%m$N0z?t$H$7$F;H$$$^$9!#(B
$B%^%/%mDj5A$O!"(B
$B%^%/%m$N(B@dfn{$BE83+7A(B}$B!J(Bexpansion$B!K$H8F$P$l$kCV49%U%)!<%`$r7W;;$7!"(B
$B$b$H$N%U%)!<%`$N$+$o$j$KE83+7A$rI>2A$7$^$9!#(B
$BE83+7A$O!"$I$s$J<oN`$N%U%)!<%`$G$b$+$^$$$^$;$s!#(B
$B<+8JI>2A7?$NDj?t!"%7%s%\%k!"$"$k$$$O!"%j%9%H$G$9!#(B
$BE83+7A$=$N$b$N$,%^%/%m8F$S=P$7$G$"$k$H!"(B
$B%^%/%m8F$S=P$70J30$N%U%)!<%`$rF@$i$l$k$^$G!"(B
$BE83+7A$rF@$k=hM}$r7+$jJV$7$^$9!#(B
@c Ordinary evaluation of a macro call finishes by evaluating the
@c expansion. However, the macro expansion is not necessarily evaluated
@c right away, or at all, because other programs also expand macro calls,
@c and they may or may not evaluate the expansions.
$BDL>o$N%^%/%m8F$S=P$7$NI>2A$O!"E83+7A$rI>2A$9$k$3$H$G40N;$7$^$9!#(B
$B$7$+$7!"%^%/%m$NE83+7A$rI,$:$7$b$?$@$A$KI>2A$9$kI,MW$O$J$/!"(B
$B$^$C$?$/I>2A$7$J$/$F$b$+$^$$$^$;$s!#(B
$B$H$$$&$N$O!"JL$N%W%m%0%i%`$b%^%/%m8F$S=P$7$rE83+$7!"(B
$B$=$l$i$OE83+7A$rI>2A$9$k$b$N$b$"$l$P!"I>2A$7$J$$$b$N$b$"$k$+$i$G$9!#(B
@c Normally, the argument expressions are not evaluated as part of
@c computing the macro expansion, but instead appear as part of the
@c expansion, so they are computed when the expansion is evaluated.
$BIaDL!"0z?t$N<0$O!"%^%/%mE83+$N7W;;2aDx$G$OI>2A$;$:!"(B
$BE83+7A$N0lIt$H$7$F8=$l$^$9!#(B
$B$=$7$F!"E83+7A$rI>2A$9$k$H$-$K0z?t$,7W;;$5$l$^$9!#(B
@c For example, given a macro defined as follows:
$B$?$H$($P!"$D$.$N$h$&$J%^%/%mDj5A$,$"$C$?$H$7$^$9!#(B
@example
@group
(defmacro cadr (x)
(list 'car (list 'cdr x)))
@end group
@end example
@noindent
@c an expression such as @code{(cadr (assq 'handler list))} is a macro
@c call, and its expansion is:
@code{(cadr (assq 'handler list))}$B$N$h$&$J<0$O%^%/%m8F$S=P$7$G$"$j!"(B
$B$D$.$N$h$&$JE83+7A$K$J$j$^$9!#(B
@example
(car (cdr (assq 'handler list)))
@end example
@noindent
@c Note that the argument @code{(assq 'handler list)} appears in the
@c expansion.
$B0z?t(B@code{(assq 'handler list)}$B$,E83+7A$K8=$l$F$$$k$3$H$K(B
$BCm0U$7$F$/$@$5$$!#(B
@c @xref{Macros}, for a complete description of Emacs Lisp macros.
Emacs Lisp$B$N%^%/%m$K4X$9$k40A4$J5-=R$O!"(B@xref{Macros}$B!#(B
@node Special Forms
@c @subsection Special Forms
@subsection $B%9%Z%7%c%k%U%)!<%`(B
@c @cindex special form evaluation
@cindex $B%9%Z%7%c%k%U%)!<%`$NI>2A(B
@cindex $BI>2A!"%9%Z%7%c%k%U%)!<%`(B
@c A @dfn{special form} is a primitive function specially marked so that
@c its arguments are not all evaluated. Most special forms define control
@c structures or perform variable bindings---things which functions cannot
@c do.
@dfn{$B%9%Z%7%c%k%U%)!<%`(B}$B!J(Bspecial form$B!K$O!"(B
$B$=$N0z?t$rI>2A$7$J$$$h$&$KFCJL$J0u$,IU$$$?4pK\4X?t$G$9!#(B
$B$[$H$s$I$N%9%Z%7%c%k%U%)!<%`$O!"@)8f9=B$$rDj5A$7$?$j!"(B
$BJQ?t$rB+G{$7$?$j$7$^$9!#(B
$B$3$l$i$O$I$l$b4X?t$G$O$G$-$J$$$3$H$G$9!#(B
@c Each special form has its own rules for which arguments are evaluated
@c and which are used without evaluation. Whether a particular argument is
@c evaluated may depend on the results of evaluating other arguments.
$B3F%9%Z%7%c%k%U%)!<%`$K$O!"$I$N0z?t$OI>2A$7!"(B
$B$I$N0z?t$OI>2A$;$:$K;H$&$+$H$$$C$?$=$l$>$l$KFH<+$N5,B'$,$"$j$^$9!#(B
$BFCDj$N0z?t$rI>2A$9$k$+$I$&$+$O!"B>$N0z?t$NI>2A7k2L$K0MB8$9$k$3$H$b$"$j$^$9!#(B
@c Here is a list, in alphabetical order, of all of the special forms in
@c Emacs Lisp with a reference to where each is described.
$B0J2<$K!"(BEmacs Lisp$B$N$9$Y$F$N%9%Z%7%c%k%U%)!<%`$r%"%k%U%!%Y%C%H=g$K!"(B
$B;2>H2U=j$H$H$b$K$"$2$F$*$-$^$9!#(B
@table @code
@item and
@pxref{Combining Conditions}
@item catch
@pxref{Catch and Throw}
@item cond
@pxref{Conditionals}
@item condition-case
@pxref{Handling Errors}
@item defconst
@pxref{Defining Variables}
@item defmacro
@pxref{Defining Macros}
@item defun
@pxref{Defining Functions}
@item defvar
@pxref{Defining Variables}
@item function
@pxref{Anonymous Functions}
@item if
@pxref{Conditionals}
@item interactive
@pxref{Interactive Call}
@item let
@itemx let*
@pxref{Local Variables}
@item or
@pxref{Combining Conditions}
@item prog1
@itemx prog2
@itemx progn
@pxref{Sequencing}
@item quote
@pxref{Quoting}
@item save-current-buffer
@pxref{Current Buffer}
@item save-excursion
@pxref{Excursions}
@item save-restriction
@pxref{Narrowing}
@item save-window-excursion
@pxref{Window Configurations}
@item setq
@pxref{Setting Variables}
@item setq-default
@pxref{Creating Buffer-Local}
@item track-mouse
@pxref{Mouse Tracking}
@item unwind-protect
@pxref{Nonlocal Exits}
@item while
@pxref{Iteration}
@item with-output-to-temp-buffer
@pxref{Temporary Displays}
@end table
@c @cindex CL note---special forms compared
@cindex CL$B$K4X$7$?Cm0U!]!]%9%Z%7%c%k%U%)!<%`$NHf3S(B
@quotation
@c @b{Common Lisp note:} Here are some comparisons of special forms in
@c GNU Emacs Lisp and Common Lisp. @code{setq}, @code{if}, and
@c @code{catch} are special forms in both Emacs Lisp and Common Lisp.
@c @code{defun} is a special form in Emacs Lisp, but a macro in Common
@c Lisp. @code{save-excursion} is a special form in Emacs Lisp, but
@c doesn't exist in Common Lisp. @code{throw} is a special form in
@c Common Lisp (because it must be able to throw multiple values), but it
@c is a function in Emacs Lisp (which doesn't have multiple
@c values).@refill
@b{Common Lisp$B$K4X$7$?Cm0U!'(B}@code{ }
GNU Emacs Lisp$B$H(BCommon Lisp$B$N%9%Z%7%c%k%U%)!<%`$rHf3S$7$F$_$k!#(B
@code{setq}$B!"(B@code{if}$B!"$*$h$S!"(B@code{catch}$B$O!"(BEmacs Lisp$B$G$b(B
Common Lisp$B$G$b%9%Z%7%c%k%U%)!<%`$G$"$k!#(B
@code{defun}$B$O!"(BEmacs Lisp$B$G$O%9%Z%7%c%k%U%)!<%`$G$"$k$,!"(B
Common Lisp$B$G$O%^%/%m$G$"$k!#(B
@code{save-excursion}$B$O!"(BEmacs Lisp$B$G$O%9%Z%7%c%k%U%)!<%`$G$"$k$,!"(B
Common Lisp$B$K$OB8:_$7$J$$!#(B
@code{throw}$B$O!"(BCommon Lisp$B$G$O!JJ#?t$NCM$rJV$9I,MW$,$"$k$?$a!K(B
$B%9%Z%7%c%k%U%)!<%`$G$"$k$,!"(B
Emacs Lisp$B$G$O!JJ#?t$NCM$O$J$$$?$a!K4X?t$G$"$k!#(B
@end quotation
@node Autoloading
@c @subsection Autoloading
@subsection $B<+F0%m!<%I(B
@c The @dfn{autoload} feature allows you to call a function or macro
@c whose function definition has not yet been loaded into Emacs. It
@c specifies which file contains the definition. When an autoload object
@c appears as a symbol's function definition, calling that symbol as a
@c function automatically loads the specified file; then it calls the real
@c definition loaded from that file. @xref{Autoload}.
@dfn{$B<+F0%m!<%I(B}$B!J(Bautoload$B!K$O!"(B
$B4X?t$d%^%/%m$N4X?tDj5A$r(BEmacs$B$K$^$@%m!<%I$7$F$$$J$/$F$b!"(B
$B4X?t$d%^%/%m$r8F$S=P$;$k$h$&$K$9$k5!9=$G$9!#(B
$BDj5A$r<}$a$?%U%!%$%k$r;XDj$7$^$9!#(B
$B%7%s%\%k$N4X?tDj5A$K<+F0%m!<%I%*%V%8%'%/%H$"$k$H$-!"(B
$B$=$N%7%s%\%k$r4X?t$H$7$F8F$S=P$9$H!";XDj$7$?%U%!%$%k$r<+F0E*$K%m!<%I$7$^$9!#(B
$B$=$&$7$F$+$i!"Ev3:%U%!%$%k$+$i%m!<%I$7$?<B:]$NDj5A$r8F$S=P$7$^$9!#(B
@xref{Autoload}$B!#(B
@node Quoting
@c @section Quoting
@section $B%/%)!<%H(B
@c @cindex quoting
@cindex $B%/%)!<%H(B
@c The special form @code{quote} returns its single argument, as written,
@c without evaluating it. This provides a way to include constant symbols
@c and lists, which are not self-evaluating objects, in a program. (It is
@c not necessary to quote self-evaluating objects such as numbers, strings,
@c and vectors.)
$B%9%Z%7%c%k%U%)!<%`(B@code{quote}$B$O!"C10l$N0z?t$r(B
$BI>2A$;$:$K=q$+$l$?$H$*$j$KJV$7$^$9!#(B
$B$3$l$O!"<+8JI>2A7?%*%V%8%'%/%H$G$O$J$$(B
$BDj?t%7%s%\%k$dDj?t%j%9%H$r%W%m%0%i%`Fb$K=q$/<jCJ$G$9!#(B
$B!J?t!"J8;zNs!"%Y%/%H%k$J$I$N<+8JI>2A7?%*%V%8%'%/%H$r%/%)!<%H$9$kI,MW$O$J$$!#!K(B
@defspec quote object
@c This special form returns @var{object}, without evaluating it.
$B$3$N%U%)!<%`$O(B@var{object}$B$rI>2A$;$:$KJV$9!#(B
@end defspec
@c @cindex @samp{'} for quoting
@c @cindex quoting using apostrophe
@c @cindex apostrophe for quoting
@cindex @samp{'}$B$K$h$k%/%)!<%H(B
@cindex $B%/%)!<%H!"(B@samp{'}
@c Because @code{quote} is used so often in programs, Lisp provides a
@c convenient read syntax for it. An apostrophe character (@samp{'})
@c followed by a Lisp object (in read syntax) expands to a list whose first
@c element is @code{quote}, and whose second element is the object. Thus,
@c the read syntax @code{'x} is an abbreviation for @code{(quote x)}.
@code{quote}$B$O%W%m%0%i%`Fb$GIQHK$K;H$&$N$G!"(B
Lisp$B$K$OJXMx$JF~NO9=J8$,MQ0U$7$F$"$j$^$9!#(B
$B%"%]%9%H%m%UJ8;z!J(B@samp{'}$B!K$KB3$1$?!JF~NO9=J8$G=q$$$?!K(BLisp$B%*%V%8%'%/%H$O!"(B
$B@hF,MWAG$,(B@code{quote}$B$G$"$j(B2$BHVL\$NMWAG$,$=$N%*%V%8%'%/%H$G$"$k(B
$B%j%9%H$KE83+$5$l$^$9!#(B
$B$7$?$,$C$F!"F~NO9=J8(B@code{'x}$B$O!"(B@code{(quote x)}$B$N>JN,7A$G$9!#(B
@c Here are some examples of expressions that use @code{quote}:
@code{quote}$B$r;H$C$?<0$NNc$r$$$/$D$+$"$2$F$*$-$^$9!#(B
@example
@group
(quote (+ 1 2))
@result{} (+ 1 2)
@end group
@group
(quote foo)
@result{} foo
@end group
@group
'foo
@result{} foo
@end group
@group
''foo
@result{} (quote foo)
@end group
@group
'(quote foo)
@result{} (quote foo)
@end group
@group
['foo]
@result{} [(quote foo)]
@end group
@end example
@c Other quoting constructs include @code{function} (@pxref{Anonymous
@c Functions}), which causes an anonymous lambda expression written in Lisp
@c to be compiled, and @samp{`} (@pxref{Backquote}), which is used to quote
@c only part of a list, while computing and substituting other parts.
$BB>$N%/%)!<%H$N=q$-J}$K$O!"(B@code{function}$B!J(B@pxref{Anonymous Functions}$B!K$,(B
$B$"$j$^$9!#(B
$B$3$l$O!"(BLisp$B$G=q$$$?L5L>%i%`%@<0$r%3%s%Q%$%k$9$k$h$&$K$7$^$9!#(B
$B$^$?!"(B@samp{`}$B!J(B@pxref{Backquote}$B!K$O!"(B
$B%j%9%H$N0lItJ,$r%/%)!<%H$7!"B>$NItJ,$O7W;;7k2L$GCV$-49$($k$?$a$K;H$$$^$9!#(B
@node Eval
@c @section Eval
@section $BI>2A!J(Beval$B!K(B
@c Most often, forms are evaluated automatically, by virtue of their
@c occurrence in a program being run. On rare occasions, you may need to
@c write code that evaluates a form that is computed at run time, such as
@c after reading a form from text being edited or getting one from a
@c property list. On these occasions, use the @code{eval} function.
$B$[$H$s$I$N>l9g!"<B9TCf$N%W%m%0%i%`$K%U%)!<%`$,8=$l$k$H(B
$B%U%)!<%`$O<+F0E*$KI>2A$5$l$^$9!#(B
$B5)$J$3$H$G$9$,!"<B9T;~$K7W;;$7$?%U%)!<%`$rI>2A$9$k$h$&$K(B
$B%3!<%I$r=q$/I,MW$,$"$k$+$b$7$l$^$;$s!#(B
$B$?$H$($P!"JT=8Cf$N%F%-%9%H$+$i%U%)!<%`$rFI$_<h$C$?$j!"(B
$BB0@-%j%9%H$+$i%U%)!<%`$r<h$j=P$7$?>l9g$J$I$G$9!#(B
$B$3$N$h$&$J>l9g$K$O!"4X?t(B@code{eval}$B$r;H$$$^$9!#(B
@c The functions and variables described in this section evaluate forms,
@c specify limits to the evaluation process, or record recently returned
@c values. Loading a file also does evaluation (@pxref{Loading}).
$BK\@a$G@bL@$7$?4X?t$dJQ?t$O!"%U%)!<%`$rI>2A$7$?$j!"(B
$BI>2A=hM}$K@)8B$r2]$7$?$j!":G8e$NLa$jCM$r5-O?$7$?$j$7$^$9!#(B
$B%U%!%$%k$r%m!<%I$7$F$bI>2A$,9T$o$l$^$9!J(B@pxref{Loading}$B!K!#(B
@c @strong{Note:} it is generally cleaner and more flexible to store a
@c function in a data structure, and call it with @code{funcall} or
@c @code{apply}, than to store an expression in the data structure and
@c evaluate it. Using functions provides the ability to pass information
@c to them as arguments.
@strong{$BCm0U!'(B}@code{ }
$B%G!<%?9=B$$NCf$K4X?t$r3JG<$7$F(B
$B$=$l$r(B@code{funcall}$B$d(B@code{apply}$B$G8F$S=P$9$[$&$,!"(B
$B%G!<%?9=B$$NCf$K<0$r3JG<$7$F$=$l$rI>2A$9$k$h$j!"(B
$B0lHL$KL@3N$G=@Fp@-$,$"$j$^$9!#(B
$B4X?t$r;H$&$H$=$l$i$K0z?t$H$7$F>pJs$rEO$9$3$H$,$G$-$^$9!#(B
@defun eval form
@c This is the basic function evaluating an expression. It evaluates
@c @var{form} in the current environment and returns the result. How the
@c evaluation proceeds depends on the type of the object (@pxref{Forms}).
$B$3$N4X?t$O!"<0$rI>2A$9$k4pK\E*$J4X?t$G$"$k!#(B
@var{form}$B$r8=:_$N4D6-$K$*$$$FI>2A$7!"$=$N7k2L$rJV$9!#(B
$BI>2A=hM}$O%*%V%8%'%/%H$N7?$K0MB8$9$k!J(B@pxref{Forms}$B!K!#(B
@c Since @code{eval} is a function, the argument expression that appears
@c in a call to @code{eval} is evaluated twice: once as preparation before
@c @code{eval} is called, and again by the @code{eval} function itself.
@c Here is an example:
@code{eval}$B$O4X?t$J$N$G!"(B@code{eval}$B$N8F$S=P$7$K8=$l$k(B
$B0z?t$N<0$O(B2$BEYI>2A$5$l$k!#(B
@code{eval}$B$r8F$S=P$9$^$($N=`Hw$G(B1$B2s!"(B
$B4X?t(B@code{eval}$B<+?H$K$h$kI>2A$G$b$&(B1$B2s$G$"$k!#(B
$BNc$r<($9!#(B
@example
@group
(setq foo 'bar)
@result{} bar
@end group
@group
(setq bar 'baz)
@result{} baz
@c ;; @r{Here @code{eval} receives argument @code{foo}}
;; @r{@code{eval}$B$O0z?t(B@code{foo}$B$r<u$1<h$k(B}
(eval 'foo)
@result{} bar
@c ;; @r{Here @code{eval} receives argument @code{bar}, which is the value of @code{foo}}
;; @r{@code{eval}$B$O0z?t(B@code{bar}$B$r<u$1<h$k!#$=$l$O(B@code{foo}$B$NCM(B}
(eval foo)
@result{} baz
@end group
@end example
@c The number of currently active calls to @code{eval} is limited to
@c @code{max-lisp-eval-depth} (see below).
@code{eval}$B$N8F$S=P$7$N?<$5$O!"(B
@code{max-lisp-eval-depth}$B!J2<5-;2>H!K$K@)8B$5$l$k!#(B
@end defun
@c @deffn Command eval-region start end &optional stream read-function
@deffn $B%3%^%s%I(B eval-region start end &optional stream read-function
@c This function evaluates the forms in the current buffer in the region
@c defined by the positions @var{start} and @var{end}. It reads forms from
@c the region and calls @code{eval} on them until the end of the region is
@c reached, or until an error is signaled and not handled.
$B$3$N4X?t$O!"%+%l%s%H%P%C%U%!$N(B@var{start}$B$H(B@var{end}$B$G;XDj$7$?(B
$B%j!<%8%g%sFb$N%U%)!<%`72$rI>2A$9$k!#(B
$B%j!<%8%g%s$+$i%U%)!<%`$rFI$_<h$j!"(B
$B$=$l$i$KBP$7$F(B@code{eval}$B$r8F$S=P$9$3$H$r(B
$B%j!<%8%g%s$NKvHx$KC#$9$k$^$G!"$"$k$$$O!"=hM}$5$l$J$$%(%i!<$,DLCN$5$l$k$^$G(B
$B7+$jJV$9!#(B
@c If @var{stream} is non-@code{nil}, the values that result from
@c evaluating the expressions in the region are printed using @var{stream}.
@c @xref{Output Streams}.
@var{stream}$B$,(B@code{nil}$B0J30$J$i$P!"(B
$B%j!<%8%g%sFb$N<0$rI>2A$7$?7k2L$NCM$O(B@var{stream}$B$r;H$C$FI=<($9$k!#(B
@pxref{Output Streams}$B!#(B
@c If @var{read-function} is non-@code{nil}, it should be a function, which
@c is used instead of @code{read} to read expressions one by one. This
@c function is called with one argument, the stream for reading input. You
@c can also use the variable @code{load-read-function} (@pxref{How Programs
@c Do Loading}) to specify this function, but it is more robust to use the
@c @var{read-function} argument.
@var{read-function}$B$,(B@code{nil}$B0J30$K$J$i$P!"(B
$B$=$l$O4X?t$G$"$kI,MW$,$"$j!"(B
@code{read}$B$N$+$o$j$K<0$r(B1$B$D(B1$B$DFI$`$?$a$K;H$o$l$k!#(B
$B$3$N4X?t$O!"F~NOMQ$N%9%H%j!<%`$G$"$k(B1$B$D$N0z?t$G8F$S=P$5$l$k!#(B
$BJQ?t(B@code{load-read-function}$B!J(B@pxref{How Programs Do Loading}$B!K$r(B
$B;H$C$F$3$N4X?t$r;XDj$9$k$3$H$b$G$-$k$,!"(B
$B0z?t(B@var{read-function}$B$rMQ$$$?$[$&$,7xO4$G$"$k!#(B
@c @code{eval-region} always returns @code{nil}.
@code{eval-region}$B$O$D$M$K(B@code{nil}$B$rJV$9!#(B
@end deffn
@c @cindex evaluation of buffer contents
@cindex $B%P%C%U%!FbMF$NI>2A(B
@cindex $BI>2A!"%P%C%U%!FbMF(B
@c @deffn Command eval-current-buffer &optional stream
@deffn $B%3%^%s%I(B eval-current-buffer &optional stream
@c This is like @code{eval-region} except that it operates on the whole
@c buffer.
$B$3$l$O(B@code{eval-region}$B$HF1MM$@$,!"%P%C%U%!A4BN$K:nMQ$9$k!#(B
@end deffn
@defvar max-lisp-eval-depth
@c This variable defines the maximum depth allowed in calls to @code{eval},
@c @code{apply}, and @code{funcall} before an error is signaled (with error
@c message @code{"Lisp nesting exceeds max-lisp-eval-depth"}). This limit,
@c with the associated error when it is exceeded, is one way that Lisp
@c avoids infinite recursion on an ill-defined function.
$B$3$NJQ?t$O!"(B
$B!J%(%i!<%a%C%;!<%8(B@code{"Lisp nesting exceeds max-lisp-eval-depth"}$B$G!K(B
$B%(%i!<$rDLCN$^$G$N(B@code{eval}$B!"(B@code{apply}$B!"(B@code{funcall}$B$N8F$S=P$7$N(B
$B:GBg$N?<$5$r@)8B$9$k!#(B
$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 @cindex Lisp nesting error
@cindex Lisp$B$NF~$l;R$N%(%i!<(B
@cindex $B%(%i!<!"(BLisp$B$NF~$l;R(B
@c The depth limit counts internal uses of @code{eval}, @code{apply}, and
@c @code{funcall}, such as for calling the functions mentioned in Lisp
@c expressions, and recursive evaluation of function call arguments and
@c function body forms, as well as explicit calls in Lisp code.
$B?<$5@)8B$O!"(BLisp$B%3!<%I$K$h$kL@<(E*$J8F$S=P$7$K2C$($F!"(B
Lisp$B<0$G=q$+$l$?4X?t$N8F$S=P$7$d4X?t8F$S=P$7$N0z?t$d4X?tK\BN$N%U%)!<%`$N(B
$B:F5"E*$JI>2A$J$I$NFbItE*$J(B@code{eval}$B!"(B@code{apply}$B!"(B@code{funcall}$B$N(B
$B8F$S=P$7$b?t$($k!#(B
@c The default value of this variable is 300. If you set it to a value
@c less than 100, Lisp will reset it to 100 if the given value is reached.
@c Entry to the Lisp debugger increases the value, if there is little room
@c left, to make sure the debugger itself has room to execute.
$B$3$NJQ?t$N%G%U%)%k%HCM$O(B300$B!#(B
$B$3$l$K(B100$BL$K~$NCM$r@_Dj$9$k$H!";XDj$7$?CM$KC#$9$k$H(BLisp$B$O(B100$B$K@_Dj$7D>$9!#(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
@c @code{max-specpdl-size} provides another limit on nesting.
@c @xref{Local Variables}.
@code{max-specpdl-size}$B$O!"F~$l;R$N?<$5$r@)8B$9$kJL$NJ}K!$G$"$k!#(B
@pxref{Local Variables}$B!#(B
@end defvar
@defvar values
@c The value of this variable is a list of the values returned by all the
@c expressions that were read, evaluated, and printed from buffers
@c (including the minibuffer) by the standard Emacs commands which do this.
@c The elements are ordered most recent first.
$B$3$NJQ?t$NCM$O!"(B
$B%P%C%U%!$+$i<0$rFI$_<h$j!"I>2A$7!"7k2L$rI=<($9$k(BEmacs$B$NI8=`%3%^%s%I$,9T$C$?(B
$B$9$Y$F$N<0$NLa$jCM$N%j%9%H$G$"$k!#(B
$B%j%9%H$N=g=x$O!":G?7$N$b$N$,:G=i$K$/$k!#(B
@example
@group
(setq x 1)
@result{} 1
@end group
@group
(list 'A (1+ 2) auto-save-default)
@result{} (A 3 t)
@end group
@group
values
@result{} ((A 3 t) 1 @dots{})
@end group
@end example
@c This variable is useful for referring back to values of forms recently
@c evaluated. It is generally a bad idea to print the value of
@c @code{values} itself, since this may be very long. Instead, examine
@c particular elements, like this:
$B$3$NJQ?t$O!":G6aI>2A$7$?%U%)!<%`$NCM$r;2>H$9$k$N$KJXMx$G$"$k!#(B
@code{values}$B$=$N$b$N$NCM$NI=<($OHs>o$KD9$/$J$k2DG=@-$,$"$k$N$G!"(B
$B$=$NCM$rI=<($9$k$N$O$h$/$J$$!#(B
$B$=$N$+$o$j$K!"$D$.$N$h$&$K$7$FFCDj$NMWAG$rD4$Y$k!#(B
@example
@group
@c ;; @r{Refer to the most recent evaluation result.}
;; @r{$B$b$C$H$b:G6a$NI>2A7k2L$r;2>H$9$k(B}
(nth 0 values)
@result{} (A 3 t)
@end group
@group
@c ;; @r{That put a new element on,}
@c ;; @r{so all elements move back one.}
;; @r{$B$3$&$9$k$H!"?7$?$JMWAG$,DI2C$5$l!"(B}
;; @r{$B$9$Y$F$NMWAG$,(B1$B$D$&$7$m$X$5$,$k(B}
(nth 1 values)
@result{} (A 3 t)
@end group
@group
@c ;; @r{This gets the element that was next-to-most-recent}
@c ;; @r{before this example.}
;; @r{$B$3$NNc$r<B9T$9$k$^$($N:G?7$N$b$N$N$D$.$NMWAG$r<hF@$9$k(B}
(nth 3 values)
@result{} 1
@end group
@end example
@end defvar
|