aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChong Yidong2013-12-25 11:05:11 +0800
committerChong Yidong2013-12-25 11:05:11 +0800
commit362397edd9f48bf431c8ea4ca2a88d15c5660bde (patch)
tree8f8a5d5486629e5ae1f1ddc52248aa064d0090f0
parent3a79600aa78d4d148c4c6b6a0a7a5ce625c582dc (diff)
downloademacs-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/ChangeLog11
-rw-r--r--doc/lispref/eval.texi18
-rw-r--r--doc/lispref/variables.texi119
-rw-r--r--etc/NEWS1
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 @@
12013-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
12013-12-24 Tassilo Horn <tsdh@gnu.org> 122013-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
717This is the basic function for evaluating an expression. It evaluates 717This 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
719evaluation proceeds depends on the type of the object (@pxref{Forms}). 719type of the @var{form} object determines how it is evaluated.
720 720@xref{Forms}.
721The argument @var{lexical}, if non-@code{nil}, means to evaluate 721
722@var{form} using lexical scoping rules for variables, instead of the 722The argument @var{lexical} specifies the scoping rule for local
723default dynamic scoping rules. @xref{Lexical Binding}. 723variables (@pxref{Variable Scoping}). If it is omitted or @code{nil},
724that means to evaluate @var{form} using the default dynamic scoping
725rule. If it is @code{t}, that means to use the lexical scoping rule.
726The value of @var{lexical} can also be a non-empty alist specifying a
727particular @dfn{lexical environment} for lexical bindings; however,
728this feature is only useful for specialized purposes, such as in Emacs
729Lisp debuggers. @xref{Lexical Binding}.
724 730
725Since @code{eval} is a function, the argument expression that appears 731Since @code{eval} is a function, the argument expression that appears
726in a call to @code{eval} is evaluated twice: once as preparation before 732in 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 @@
10In Lisp, each variable is represented by a Lisp symbol 10In 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
12the variable's value is stored in the symbol's value cell@footnote{To 12the variable's value is stored in the symbol's value cell@footnote{To
13be precise, under the default @dfn{dynamic binding} rules the value 13be precise, under the default @dfn{dynamic scoping} rule, the value
14cell always holds the variable's current value, but this is not the 14cell always holds the variable's current value, but this is not the
15case under @dfn{lexical binding} rules. @xref{Variable Scoping}, for 15case under the @dfn{lexical scoping} rule. @xref{Variable Scoping},
16details.}. @xref{Symbol Components}. In Emacs Lisp, the use of a 16for details.}. @xref{Symbol Components}. In Emacs Lisp, the use of a
17symbol as a variable is independent of its use as a function name. 17symbol 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
295cell (@pxref{Symbol Components}). Under Emacs Lisp's default dynamic 295cell (@pxref{Symbol Components}).
296binding rules (@pxref{Variable Scoping}), the value cell stores the 296
297variable's current (local or global) value. Note that an unassigned 297 Under Emacs Lisp's default dynamic scoping rule (@pxref{Variable
298value cell is @emph{not} the same as having @code{nil} in the value 298Scoping}), the value cell stores the variable's current (local or
299cell. The symbol @code{nil} is a Lisp object and can be the value of 299global) value. Note that an unassigned value cell is @emph{not} the
300a variable, just as any other object can be; but it is still a value. 300same as having @code{nil} in the value cell. The symbol @code{nil} is
301If a variable is void, trying to evaluate the variable signals a 301a Lisp object and can be the value of a variable, just as any other
302@code{void-variable} error rather than a value. 302object can be; but it is still a value. If a variable is void, trying
303 303to evaluate the variable signals a @code{void-variable} error, instead
304 Under lexical binding rules, the value cell only holds the 304of returning a value.
305variable's global value, i.e., the value outside of any lexical 305
306binding construct. When a variable is lexically bound, the local value 306 Under the optional lexical scoping rule, the value cell only holds
307is determined by the lexical environment; the variable may have a 307the variable's global value---the value outside of any lexical binding
308local value if its symbol's value cell is unassigned. 308construct. When a variable is lexically bound, the local value is
309determined by the lexical environment; hence, variables can have local
310values even if their symbols' value cells are unassigned.
309 311
310@defun makunbound symbol 312@defun makunbound symbol
311This function empties out the value cell of @var{symbol}, making the 313This 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
766effect only within a limited portion of the program (@pxref{Local 769effect 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
774program is executing, the binding exists. 777program 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
780bindings}. Such a binding has @dfn{indefinite scope}, meaning that 783bindings}. Such a binding has @dfn{dynamic scope}, meaning that any
781any part of the program can potentially access the variable binding. 784part of the program can potentially access the variable binding. It
782It also has @dfn{dynamic extent}, meaning that the binding lasts only 785also has @dfn{dynamic extent}, meaning that the binding lasts only
783while the binding construct (such as the body of a @code{let} form) is 786while the binding construct (such as the body of a @code{let} form) is
784being executed. 787being 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
790binding has @dfn{lexical scope}, meaning that any reference to the 793binding has @dfn{lexical scope}, meaning that any reference to the
791variable must be located textually within the binding construct. It 794variable must be located textually within the binding
792also has @dfn{indefinite extent}, meaning that under some 795construct@footnote{With some exceptions; for instance, a lexical
793circumstances the binding can live on even after the binding construct 796binding can also be accessed from the Lisp debugger.}. It also has
794has finished executing, by means of special objects called 797@dfn{indefinite extent}, meaning that under some circumstances the
795@dfn{closures}. 798binding can live on even after the binding construct has finished
799executing, 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
798binding in greater detail, and how to enable lexical binding in Emacs 802binding 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
814recently-created dynamic local binding for that symbol, or the global 818recently-created dynamic local binding for that symbol, or the global
815binding if there is no such local binding. 819binding 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
818by the following example: 822following 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''
841reference, in the sense that there is no binding for @code{x} within 845reference, in the sense that there is no binding for @code{x} within
842that @code{defun} construct itself. When we call @code{getx} from 846that @code{defun} construct itself. When we call @code{getx} from
843within a @code{let} form in which @code{x} is (dynamically) bound, it 847within a @code{let} form in which @code{x} is (dynamically) bound, it
844retrieves the local value of @code{x} (i.e., 1). But when we call 848retrieves 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 849outside the @code{let} form, it retrieves the global value (i.e.,
846of @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
849bound variable using @code{setq}: 853bound variable using @code{setq}:
@@ -888,12 +892,11 @@ technique:
888@itemize @bullet 892@itemize @bullet
889@item 893@item
890If a variable has no global definition, use it as a local variable 894If a variable has no global definition, use it as a local variable
891only within a binding construct, e.g., the body of the @code{let} 895only within a binding construct, such as the body of the @code{let}
892form where the variable was bound, or the body of the function for an 896form where the variable was bound. If this convention is followed
893argument variable. If this convention is followed consistently 897consistently throughout a program, the value of the variable will not
894throughout a program, the value of the variable will not affect, nor 898affect, nor be affected by, any uses of the same variable symbol
895be affected by, any uses of the same variable symbol elsewhere in the 899elsewhere in the program.
896program.
897 900
898@item 901@item
899Otherwise, define the variable with @code{defvar}, @code{defconst}, or 902Otherwise, 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
928Optionally, you can create lexical bindings in Emacs Lisp. A 931 Lexical binding was introduced to Emacs, as an optional feature, in
929lexically bound variable has @dfn{lexical scope}, meaning that any 932version 24.1. We expect its importance to increase in the future.
930reference to the variable must be located textually within the binding 933Lexical binding opens up many more opportunities for optimization, so
931construct. 934programs using it are likely to run faster in future Emacs versions.
935Lexical binding is also more compatible with concurrency, which we
936want 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
939reference to the variable must be located textually within the binding
940construct. 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
969environment; if the variable is not specified in there, it looks in 976environment; if the variable is not specified in there, it looks in
970the symbol's value cell, where the dynamic value is stored. 977the symbol's value cell, where the dynamic value is stored.
971 978
979 (Internally, the lexical environment is an alist of symbol-value
980pairs, with the final element in the alist being the symbol @code{t}
981rather than a cons cell. Such an alist can be passed as the second
982argument to the @code{eval} function, in order to specify a lexical
983environment in which to evaluate a form. @xref{Eval}. Most Emacs
984Lisp programs, however, should not interact directly with lexical
985environments 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
974construct has finished executing, its lexical environment can be 989construct has finished executing, its lexical environment can be
@@ -1019,13 +1034,6 @@ binding of @code{x} in that lexical environment.
1019the body of a @code{defun} or @code{defmacro} cannot refer to 1034the body of a @code{defun} or @code{defmacro} cannot refer to
1020surrounding lexical variables. 1035surrounding lexical variables.
1021 1036
1022 Currently, lexical binding is not much used within the Emacs
1023sources. However, we expect its importance to increase in the future.
1024Lexical binding opens up a lot more opportunities for optimization, so
1025Emacs Lisp code that makes use of lexical binding is likely to run
1026faster in future Emacs versions. Such code is also much more friendly
1027to 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
1069binding mode is enabled (it may use lexical binding sometimes, and 1077binding mode is enabled (it may use lexical binding sometimes, and
1070dynamic binding other times). 1078dynamic 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,
1073First, add a file-local variable setting of @code{lexical-binding} to 1081add 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
1075variable in the program which needs to be dynamically bound has a 1083Local Variables}). Second, check that every variable in the program
1076variable definition, so that it is not inadvertently bound lexically. 1084which needs to be dynamically bound has a variable definition, so that
1085it 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
1079is to byte-compile the source file. @xref{Byte Compilation}. If a 1090is to byte-compile the source file. @xref{Byte Compilation}. If a
1080non-special variable is used outside of a @code{let} form, the 1091non-special variable is used outside of a @code{let} form, the
diff --git a/etc/NEWS b/etc/NEWS
index 2e6da37590c..00a72f03e0d 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -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+++