diff options
| author | Chong Yidong | 2013-12-25 11:05:11 +0800 |
|---|---|---|
| committer | Chong Yidong | 2013-12-25 11:05:11 +0800 |
| commit | 362397edd9f48bf431c8ea4ca2a88d15c5660bde (patch) | |
| tree | 8f8a5d5486629e5ae1f1ddc52248aa064d0090f0 | |
| parent | 3a79600aa78d4d148c4c6b6a0a7a5ce625c582dc (diff) | |
| download | emacs-362397edd9f48bf431c8ea4ca2a88d15c5660bde.tar.gz emacs-362397edd9f48bf431c8ea4ca2a88d15c5660bde.zip | |
Document `eval' changes.
* doc/lispref/eval.texi (Eval): Document the LEXICAL arg to eval.
* doc/lispref/variables.texi (Variables, Void Variables): Use "scoping rule"
terminology consistently.
(Variable Scoping): Add index entries, and use "dynamic scope"
terminology in place of "indefinite scope" to reduce confusion.
(Lexical Binding): Document lexical environment format.
(Using Lexical Binding): Add index entries for error messages.
| -rw-r--r-- | doc/lispref/ChangeLog | 11 | ||||
| -rw-r--r-- | doc/lispref/eval.texi | 18 | ||||
| -rw-r--r-- | doc/lispref/variables.texi | 119 | ||||
| -rw-r--r-- | etc/NEWS | 1 |
4 files changed, 89 insertions, 60 deletions
diff --git a/doc/lispref/ChangeLog b/doc/lispref/ChangeLog index b1c210834d2..595102bb5ef 100644 --- a/doc/lispref/ChangeLog +++ b/doc/lispref/ChangeLog | |||
| @@ -1,3 +1,14 @@ | |||
| 1 | 2013-12-25 Chong Yidong <cyd@gnu.org> | ||
| 2 | |||
| 3 | * eval.texi (Eval): Document the LEXICAL arg to eval. | ||
| 4 | |||
| 5 | * variables.texi (Variables, Void Variables): Use "scoping rule" | ||
| 6 | terminology consistently. | ||
| 7 | (Variable Scoping): Add index entries, and use "dynamic scope" | ||
| 8 | terminology in place of "indefinite scope" to reduce confusion. | ||
| 9 | (Lexical Binding): Document lexical environment format. | ||
| 10 | (Using Lexical Binding): Add index entries for error messages. | ||
| 11 | |||
| 1 | 2013-12-24 Tassilo Horn <tsdh@gnu.org> | 12 | 2013-12-24 Tassilo Horn <tsdh@gnu.org> |
| 2 | 13 | ||
| 3 | * control.texi (Pattern matching case statement): Fix missing | 14 | * control.texi (Pattern matching case statement): Fix missing |
diff --git a/doc/lispref/eval.texi b/doc/lispref/eval.texi index df30b909cbd..4a63ec2add1 100644 --- a/doc/lispref/eval.texi +++ b/doc/lispref/eval.texi | |||
| @@ -715,12 +715,18 @@ arguments. | |||
| 715 | 715 | ||
| 716 | @defun eval form &optional lexical | 716 | @defun eval form &optional lexical |
| 717 | This is the basic function for evaluating an expression. It evaluates | 717 | This is the basic function for evaluating an expression. It evaluates |
| 718 | @var{form} in the current environment and returns the result. How the | 718 | @var{form} in the current environment, and returns the result. The |
| 719 | evaluation proceeds depends on the type of the object (@pxref{Forms}). | 719 | type of the @var{form} object determines how it is evaluated. |
| 720 | 720 | @xref{Forms}. | |
| 721 | The argument @var{lexical}, if non-@code{nil}, means to evaluate | 721 | |
| 722 | @var{form} using lexical scoping rules for variables, instead of the | 722 | The argument @var{lexical} specifies the scoping rule for local |
| 723 | default dynamic scoping rules. @xref{Lexical Binding}. | 723 | variables (@pxref{Variable Scoping}). If it is omitted or @code{nil}, |
| 724 | that means to evaluate @var{form} using the default dynamic scoping | ||
| 725 | rule. If it is @code{t}, that means to use the lexical scoping rule. | ||
| 726 | The value of @var{lexical} can also be a non-empty alist specifying a | ||
| 727 | particular @dfn{lexical environment} for lexical bindings; however, | ||
| 728 | this feature is only useful for specialized purposes, such as in Emacs | ||
| 729 | Lisp debuggers. @xref{Lexical Binding}. | ||
| 724 | 730 | ||
| 725 | Since @code{eval} is a function, the argument expression that appears | 731 | Since @code{eval} is a function, the argument expression that appears |
| 726 | in a call to @code{eval} is evaluated twice: once as preparation before | 732 | in a call to @code{eval} is evaluated twice: once as preparation before |
diff --git a/doc/lispref/variables.texi b/doc/lispref/variables.texi index 941b2d880e4..5d1a47661aa 100644 --- a/doc/lispref/variables.texi +++ b/doc/lispref/variables.texi | |||
| @@ -10,10 +10,10 @@ | |||
| 10 | In Lisp, each variable is represented by a Lisp symbol | 10 | In Lisp, each variable is represented by a Lisp symbol |
| 11 | (@pxref{Symbols}). The variable name is simply the symbol's name, and | 11 | (@pxref{Symbols}). The variable name is simply the symbol's name, and |
| 12 | the variable's value is stored in the symbol's value cell@footnote{To | 12 | the variable's value is stored in the symbol's value cell@footnote{To |
| 13 | be precise, under the default @dfn{dynamic binding} rules the value | 13 | be precise, under the default @dfn{dynamic scoping} rule, the value |
| 14 | cell always holds the variable's current value, but this is not the | 14 | cell always holds the variable's current value, but this is not the |
| 15 | case under @dfn{lexical binding} rules. @xref{Variable Scoping}, for | 15 | case under the @dfn{lexical scoping} rule. @xref{Variable Scoping}, |
| 16 | details.}. @xref{Symbol Components}. In Emacs Lisp, the use of a | 16 | for details.}. @xref{Symbol Components}. In Emacs Lisp, the use of a |
| 17 | symbol as a variable is independent of its use as a function name. | 17 | symbol as a variable is independent of its use as a function name. |
| 18 | 18 | ||
| 19 | As previously noted in this manual, a Lisp program is represented | 19 | As previously noted in this manual, a Lisp program is represented |
| @@ -292,20 +292,22 @@ has room to execute. | |||
| 292 | @cindex void variable | 292 | @cindex void variable |
| 293 | 293 | ||
| 294 | We say that a variable is void if its symbol has an unassigned value | 294 | We say that a variable is void if its symbol has an unassigned value |
| 295 | cell (@pxref{Symbol Components}). Under Emacs Lisp's default dynamic | 295 | cell (@pxref{Symbol Components}). |
| 296 | binding rules (@pxref{Variable Scoping}), the value cell stores the | 296 | |
| 297 | variable's current (local or global) value. Note that an unassigned | 297 | Under Emacs Lisp's default dynamic scoping rule (@pxref{Variable |
| 298 | value cell is @emph{not} the same as having @code{nil} in the value | 298 | Scoping}), the value cell stores the variable's current (local or |
| 299 | cell. The symbol @code{nil} is a Lisp object and can be the value of | 299 | global) value. Note that an unassigned value cell is @emph{not} the |
| 300 | a variable, just as any other object can be; but it is still a value. | 300 | same as having @code{nil} in the value cell. The symbol @code{nil} is |
| 301 | If a variable is void, trying to evaluate the variable signals a | 301 | a Lisp object and can be the value of a variable, just as any other |
| 302 | @code{void-variable} error rather than a value. | 302 | object can be; but it is still a value. If a variable is void, trying |
| 303 | 303 | to evaluate the variable signals a @code{void-variable} error, instead | |
| 304 | Under lexical binding rules, the value cell only holds the | 304 | of returning a value. |
| 305 | variable's global value, i.e., the value outside of any lexical | 305 | |
| 306 | binding construct. When a variable is lexically bound, the local value | 306 | Under the optional lexical scoping rule, the value cell only holds |
| 307 | is determined by the lexical environment; the variable may have a | 307 | the variable's global value---the value outside of any lexical binding |
| 308 | local value if its symbol's value cell is unassigned. | 308 | construct. When a variable is lexically bound, the local value is |
| 309 | determined by the lexical environment; hence, variables can have local | ||
| 310 | values even if their symbols' value cells are unassigned. | ||
| 309 | 311 | ||
| 310 | @defun makunbound symbol | 312 | @defun makunbound symbol |
| 311 | This function empties out the value cell of @var{symbol}, making the | 313 | This function empties out the value cell of @var{symbol}, making the |
| @@ -761,6 +763,7 @@ error is signaled. | |||
| 761 | 763 | ||
| 762 | @node Variable Scoping | 764 | @node Variable Scoping |
| 763 | @section Scoping Rules for Variable Bindings | 765 | @section Scoping Rules for Variable Bindings |
| 766 | @cindex scoping rule | ||
| 764 | 767 | ||
| 765 | When you create a local binding for a variable, that binding takes | 768 | When you create a local binding for a variable, that binding takes |
| 766 | effect only within a limited portion of the program (@pxref{Local | 769 | effect only within a limited portion of the program (@pxref{Local |
| @@ -774,12 +777,12 @@ binding can be accessed. @dfn{Extent} refers to @emph{when}, as the | |||
| 774 | program is executing, the binding exists. | 777 | program is executing, the binding exists. |
| 775 | 778 | ||
| 776 | @cindex dynamic binding | 779 | @cindex dynamic binding |
| 777 | @cindex indefinite scope | 780 | @cindex dynamic scope |
| 778 | @cindex dynamic extent | 781 | @cindex dynamic extent |
| 779 | By default, the local bindings that Emacs creates are @dfn{dynamic | 782 | By default, the local bindings that Emacs creates are @dfn{dynamic |
| 780 | bindings}. Such a binding has @dfn{indefinite scope}, meaning that | 783 | bindings}. Such a binding has @dfn{dynamic scope}, meaning that any |
| 781 | any part of the program can potentially access the variable binding. | 784 | part of the program can potentially access the variable binding. It |
| 782 | It also has @dfn{dynamic extent}, meaning that the binding lasts only | 785 | also has @dfn{dynamic extent}, meaning that the binding lasts only |
| 783 | while the binding construct (such as the body of a @code{let} form) is | 786 | while the binding construct (such as the body of a @code{let} form) is |
| 784 | being executed. | 787 | being executed. |
| 785 | 788 | ||
| @@ -788,11 +791,12 @@ being executed. | |||
| 788 | @cindex indefinite extent | 791 | @cindex indefinite extent |
| 789 | Emacs can optionally create @dfn{lexical bindings}. A lexical | 792 | Emacs can optionally create @dfn{lexical bindings}. A lexical |
| 790 | binding has @dfn{lexical scope}, meaning that any reference to the | 793 | binding has @dfn{lexical scope}, meaning that any reference to the |
| 791 | variable must be located textually within the binding construct. It | 794 | variable must be located textually within the binding |
| 792 | also has @dfn{indefinite extent}, meaning that under some | 795 | construct@footnote{With some exceptions; for instance, a lexical |
| 793 | circumstances the binding can live on even after the binding construct | 796 | binding can also be accessed from the Lisp debugger.}. It also has |
| 794 | has finished executing, by means of special objects called | 797 | @dfn{indefinite extent}, meaning that under some circumstances the |
| 795 | @dfn{closures}. | 798 | binding can live on even after the binding construct has finished |
| 799 | executing, by means of special objects called @dfn{closures}. | ||
| 796 | 800 | ||
| 797 | The following subsections describe dynamic binding and lexical | 801 | The following subsections describe dynamic binding and lexical |
| 798 | binding in greater detail, and how to enable lexical binding in Emacs | 802 | binding in greater detail, and how to enable lexical binding in Emacs |
| @@ -814,8 +818,8 @@ at any point in the execution of the Lisp program is simply the most | |||
| 814 | recently-created dynamic local binding for that symbol, or the global | 818 | recently-created dynamic local binding for that symbol, or the global |
| 815 | binding if there is no such local binding. | 819 | binding if there is no such local binding. |
| 816 | 820 | ||
| 817 | Dynamic bindings have indefinite scope and dynamic extent, as shown | 821 | Dynamic bindings have dynamic scope and extent, as shown by the |
| 818 | by the following example: | 822 | following example: |
| 819 | 823 | ||
| 820 | @example | 824 | @example |
| 821 | @group | 825 | @group |
| @@ -841,9 +845,9 @@ The function @code{getx} refers to @code{x}. This is a ``free'' | |||
| 841 | reference, in the sense that there is no binding for @code{x} within | 845 | reference, in the sense that there is no binding for @code{x} within |
| 842 | that @code{defun} construct itself. When we call @code{getx} from | 846 | that @code{defun} construct itself. When we call @code{getx} from |
| 843 | within a @code{let} form in which @code{x} is (dynamically) bound, it | 847 | within a @code{let} form in which @code{x} is (dynamically) bound, it |
| 844 | retrieves the local value of @code{x} (i.e., 1). But when we call | 848 | retrieves the local value (i.e., 1). But when we call @code{getx} |
| 845 | @code{getx} outside the @code{let} form, it retrieves the global value | 849 | outside the @code{let} form, it retrieves the global value (i.e., |
| 846 | of @code{x} (i.e., -99). | 850 | -99). |
| 847 | 851 | ||
| 848 | Here is another example, which illustrates setting a dynamically | 852 | Here is another example, which illustrates setting a dynamically |
| 849 | bound variable using @code{setq}: | 853 | bound variable using @code{setq}: |
| @@ -888,12 +892,11 @@ technique: | |||
| 888 | @itemize @bullet | 892 | @itemize @bullet |
| 889 | @item | 893 | @item |
| 890 | If a variable has no global definition, use it as a local variable | 894 | If a variable has no global definition, use it as a local variable |
| 891 | only within a binding construct, e.g., the body of the @code{let} | 895 | only within a binding construct, such as the body of the @code{let} |
| 892 | form where the variable was bound, or the body of the function for an | 896 | form where the variable was bound. If this convention is followed |
| 893 | argument variable. If this convention is followed consistently | 897 | consistently throughout a program, the value of the variable will not |
| 894 | throughout a program, the value of the variable will not affect, nor | 898 | affect, nor be affected by, any uses of the same variable symbol |
| 895 | be affected by, any uses of the same variable symbol elsewhere in the | 899 | elsewhere in the program. |
| 896 | program. | ||
| 897 | 900 | ||
| 898 | @item | 901 | @item |
| 899 | Otherwise, define the variable with @code{defvar}, @code{defconst}, or | 902 | Otherwise, define the variable with @code{defvar}, @code{defconst}, or |
| @@ -925,12 +928,16 @@ variables like @code{case-fold-search}: | |||
| 925 | @node Lexical Binding | 928 | @node Lexical Binding |
| 926 | @subsection Lexical Binding | 929 | @subsection Lexical Binding |
| 927 | 930 | ||
| 928 | Optionally, you can create lexical bindings in Emacs Lisp. A | 931 | Lexical binding was introduced to Emacs, as an optional feature, in |
| 929 | lexically bound variable has @dfn{lexical scope}, meaning that any | 932 | version 24.1. We expect its importance to increase in the future. |
| 930 | reference to the variable must be located textually within the binding | 933 | Lexical binding opens up many more opportunities for optimization, so |
| 931 | construct. | 934 | programs using it are likely to run faster in future Emacs versions. |
| 935 | Lexical binding is also more compatible with concurrency, which we | ||
| 936 | want to add to Emacs in the future. | ||
| 932 | 937 | ||
| 933 | Here is an example | 938 | A lexically-bound variable has @dfn{lexical scope}, meaning that any |
| 939 | reference to the variable must be located textually within the binding | ||
| 940 | construct. Here is an example | ||
| 934 | @iftex | 941 | @iftex |
| 935 | (see the next subsection, for how to actually enable lexical binding): | 942 | (see the next subsection, for how to actually enable lexical binding): |
| 936 | @end iftex | 943 | @end iftex |
| @@ -969,6 +976,14 @@ wants the current value of a variable, it looks first in the lexical | |||
| 969 | environment; if the variable is not specified in there, it looks in | 976 | environment; if the variable is not specified in there, it looks in |
| 970 | the symbol's value cell, where the dynamic value is stored. | 977 | the symbol's value cell, where the dynamic value is stored. |
| 971 | 978 | ||
| 979 | (Internally, the lexical environment is an alist of symbol-value | ||
| 980 | pairs, with the final element in the alist being the symbol @code{t} | ||
| 981 | rather than a cons cell. Such an alist can be passed as the second | ||
| 982 | argument to the @code{eval} function, in order to specify a lexical | ||
| 983 | environment in which to evaluate a form. @xref{Eval}. Most Emacs | ||
| 984 | Lisp programs, however, should not interact directly with lexical | ||
| 985 | environments in this way; only specialized programs like debuggers.) | ||
| 986 | |||
| 972 | @cindex closures, example of using | 987 | @cindex closures, example of using |
| 973 | Lexical bindings have indefinite extent. Even after a binding | 988 | Lexical bindings have indefinite extent. Even after a binding |
| 974 | construct has finished executing, its lexical environment can be | 989 | construct has finished executing, its lexical environment can be |
| @@ -1019,13 +1034,6 @@ binding of @code{x} in that lexical environment. | |||
| 1019 | the body of a @code{defun} or @code{defmacro} cannot refer to | 1034 | the body of a @code{defun} or @code{defmacro} cannot refer to |
| 1020 | surrounding lexical variables. | 1035 | surrounding lexical variables. |
| 1021 | 1036 | ||
| 1022 | Currently, lexical binding is not much used within the Emacs | ||
| 1023 | sources. However, we expect its importance to increase in the future. | ||
| 1024 | Lexical binding opens up a lot more opportunities for optimization, so | ||
| 1025 | Emacs Lisp code that makes use of lexical binding is likely to run | ||
| 1026 | faster in future Emacs versions. Such code is also much more friendly | ||
| 1027 | to concurrency, which we want to add to Emacs in the near future. | ||
| 1028 | |||
| 1029 | @node Using Lexical Binding | 1037 | @node Using Lexical Binding |
| 1030 | @subsection Using Lexical Binding | 1038 | @subsection Using Lexical Binding |
| 1031 | 1039 | ||
| @@ -1069,12 +1077,15 @@ discouraged. Doing so gives rise to unspecified behavior when lexical | |||
| 1069 | binding mode is enabled (it may use lexical binding sometimes, and | 1077 | binding mode is enabled (it may use lexical binding sometimes, and |
| 1070 | dynamic binding other times). | 1078 | dynamic binding other times). |
| 1071 | 1079 | ||
| 1072 | Converting an Emacs Lisp program to lexical binding is pretty easy. | 1080 | Converting an Emacs Lisp program to lexical binding is easy. First, |
| 1073 | First, add a file-local variable setting of @code{lexical-binding} to | 1081 | add a file-local variable setting of @code{lexical-binding} to |
| 1074 | @code{t} in the Emacs Lisp source file. Second, check that every | 1082 | @code{t} in the header line of the Emacs Lisp source file (@pxref{File |
| 1075 | variable in the program which needs to be dynamically bound has a | 1083 | Local Variables}). Second, check that every variable in the program |
| 1076 | variable definition, so that it is not inadvertently bound lexically. | 1084 | which needs to be dynamically bound has a variable definition, so that |
| 1085 | it is not inadvertently bound lexically. | ||
| 1077 | 1086 | ||
| 1087 | @cindex free variable | ||
| 1088 | @cindex unused lexical variable | ||
| 1078 | A simple way to find out which variables need a variable definition | 1089 | A simple way to find out which variables need a variable definition |
| 1079 | is to byte-compile the source file. @xref{Byte Compilation}. If a | 1090 | is to byte-compile the source file. @xref{Byte Compilation}. If a |
| 1080 | non-special variable is used outside of a @code{let} form, the | 1091 | non-special variable is used outside of a @code{let} form, the |
| @@ -916,6 +916,7 @@ something (not just adding elements to it). | |||
| 916 | 916 | ||
| 917 | * Lisp Changes in Emacs 24.4 | 917 | * Lisp Changes in Emacs 24.4 |
| 918 | 918 | ||
| 919 | +++ | ||
| 919 | ** The second argument of `eval' can now specify a lexical environment. | 920 | ** The second argument of `eval' can now specify a lexical environment. |
| 920 | 921 | ||
| 921 | +++ | 922 | +++ |