aboutsummaryrefslogtreecommitdiffstats
path: root/doc
diff options
context:
space:
mode:
Diffstat (limited to 'doc')
-rw-r--r--doc/lispref/ChangeLog6
-rw-r--r--doc/lispref/elisp.texi3
-rw-r--r--doc/lispref/eval.texi10
-rw-r--r--doc/lispref/variables.texi111
4 files changed, 97 insertions, 33 deletions
diff --git a/doc/lispref/ChangeLog b/doc/lispref/ChangeLog
index c5e445cec38..338dbb5e7fd 100644
--- a/doc/lispref/ChangeLog
+++ b/doc/lispref/ChangeLog
@@ -1,3 +1,9 @@
12011-03-01 Stefan Monnier <monnier@iro.umontreal.ca>
2
3 * variables.texi (Scope): Mention the availability of lexical scoping.
4 (Lexical Binding): New node.
5 * eval.texi (Eval): Add `eval's new `lexical' arg.
6
12011-02-25 Stefan Monnier <monnier@iro.umontreal.ca> 72011-02-25 Stefan Monnier <monnier@iro.umontreal.ca>
2 8
3 * vol2.texi (Top): 9 * vol2.texi (Top):
diff --git a/doc/lispref/elisp.texi b/doc/lispref/elisp.texi
index f7c1d55f6ae..cc3ceb8003c 100644
--- a/doc/lispref/elisp.texi
+++ b/doc/lispref/elisp.texi
@@ -466,7 +466,8 @@ Functions
466* Declaring Functions:: Telling the compiler that a function is defined. 466* Declaring Functions:: Telling the compiler that a function is defined.
467* Function Safety:: Determining whether a function is safe to call. 467* Function Safety:: Determining whether a function is safe to call.
468* Related Topics:: Cross-references to specific Lisp primitives 468* Related Topics:: Cross-references to specific Lisp primitives
469 that have a special bearing on how functions work. 469 that have a special bearing on how
470 functions work.
470 471
471Lambda Expressions 472Lambda Expressions
472 473
diff --git a/doc/lispref/eval.texi b/doc/lispref/eval.texi
index d44fe5bb95b..74f3d9c48b9 100644
--- a/doc/lispref/eval.texi
+++ b/doc/lispref/eval.texi
@@ -585,6 +585,11 @@ occurrence in a program being run. On rare occasions, you may need to
585write code that evaluates a form that is computed at run time, such as 585write code that evaluates a form that is computed at run time, such as
586after reading a form from text being edited or getting one from a 586after reading a form from text being edited or getting one from a
587property list. On these occasions, use the @code{eval} function. 587property list. On these occasions, use the @code{eval} function.
588Often @code{eval} is not needed and something else should be used instead.
589For example, to get the value of a variable, while @code{eval} works,
590@code{symbol-value} is preferable; or rather than store expressions
591in a property list that then need to go through @code{eval}, it is better to
592store functions instead that are then passed to @code{funcall}.
588 593
589 The functions and variables described in this section evaluate forms, 594 The functions and variables described in this section evaluate forms,
590specify limits to the evaluation process, or record recently returned 595specify limits to the evaluation process, or record recently returned
@@ -596,10 +601,13 @@ to store an expression in the data structure and evaluate it. Using
596functions provides the ability to pass information to them as 601functions provides the ability to pass information to them as
597arguments. 602arguments.
598 603
599@defun eval form 604@defun eval form &optional lexical
600This is the basic function evaluating an expression. It evaluates 605This is the basic function evaluating an expression. It evaluates
601@var{form} in the current environment and returns the result. How the 606@var{form} in the current environment and returns the result. How the
602evaluation proceeds depends on the type of the object (@pxref{Forms}). 607evaluation proceeds depends on the type of the object (@pxref{Forms}).
608@var{lexical} if non-nil means to evaluate @var{form} using lexical scoping
609rules (@pxref{Lexical Binding}) instead of the default dynamic scoping used
610historically in Emacs Lisp.
603 611
604Since @code{eval} is a function, the argument expression that appears 612Since @code{eval} is a function, the argument expression that appears
605in a call to @code{eval} is evaluated twice: once as preparation before 613in 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 0cdcaa84d58..edffb4742ec 100644
--- a/doc/lispref/variables.texi
+++ b/doc/lispref/variables.texi
@@ -25,22 +25,22 @@ textual Lisp program is written using the read syntax for the symbol
25representing the variable. 25representing the variable.
26 26
27@menu 27@menu
28* Global Variables:: Variable values that exist permanently, everywhere. 28* Global Variables:: Variable values that exist permanently, everywhere.
29* Constant Variables:: Certain "variables" have values that never change. 29* Constant Variables:: Certain "variables" have values that never change.
30* Local Variables:: Variable values that exist only temporarily. 30* Local Variables:: Variable values that exist only temporarily.
31* Void Variables:: Symbols that lack values. 31* Void Variables:: Symbols that lack values.
32* Defining Variables:: A definition says a symbol is used as a variable. 32* Defining Variables:: A definition says a symbol is used as a variable.
33* Tips for Defining:: Things you should think about when you 33* Tips for Defining:: Things you should think about when you
34 define a variable. 34 define a variable.
35* Accessing Variables:: Examining values of variables whose names 35* Accessing Variables:: Examining values of variables whose names
36 are known only at run time. 36 are known only at run time.
37* Setting Variables:: Storing new values in variables. 37* Setting Variables:: Storing new values in variables.
38* Variable Scoping:: How Lisp chooses among local and global values. 38* Variable Scoping:: How Lisp chooses among local and global values.
39* Buffer-Local Variables:: Variable values in effect only in one buffer. 39* Buffer-Local Variables:: Variable values in effect only in one buffer.
40* File Local Variables:: Handling local variable lists in files. 40* File Local Variables:: Handling local variable lists in files.
41* Directory Local Variables:: Local variables common to all files in a directory. 41* Directory Local Variables:: Local variables common to all files in a directory.
42* Frame-Local Variables:: Frame-local bindings for variables. 42* Frame-Local Variables:: Frame-local bindings for variables.
43* Variable Aliases:: Variables that are aliases for other variables. 43* Variable Aliases:: Variables that are aliases for other variables.
44* Variables with Restricted Values:: Non-constant variables whose value can 44* Variables with Restricted Values:: Non-constant variables whose value can
45 @emph{not} be an arbitrary Lisp object. 45 @emph{not} be an arbitrary Lisp object.
46@end menu 46@end menu
@@ -437,14 +437,18 @@ this reason, user options must be defined with @code{defvar}.
437This special form defines @var{symbol} as a variable and can also 437This special form defines @var{symbol} as a variable and can also
438initialize and document it. The definition informs a person reading 438initialize and document it. The definition informs a person reading
439your code that @var{symbol} is used as a variable that might be set or 439your code that @var{symbol} is used as a variable that might be set or
440changed. Note that @var{symbol} is not evaluated; the symbol to be 440changed. It also declares this variable as @dfn{special}, meaning that it
441defined must appear explicitly in the @code{defvar}. 441should always use dynamic scoping rules. Note that @var{symbol} is not
442evaluated; the symbol to be defined must appear explicitly in the
443@code{defvar}.
442 444
443If @var{symbol} is void and @var{value} is specified, @code{defvar} 445If @var{symbol} is void and @var{value} is specified, @code{defvar}
444evaluates it and sets @var{symbol} to the result. But if @var{symbol} 446evaluates it and sets @var{symbol} to the result. But if @var{symbol}
445already has a value (i.e., it is not void), @var{value} is not even 447already has a value (i.e., it is not void), @var{value} is not even
446evaluated, and @var{symbol}'s value remains unchanged. If @var{value} 448evaluated, and @var{symbol}'s value remains unchanged.
447is omitted, the value of @var{symbol} is not changed in any case. 449If @var{value} is omitted, the value of @var{symbol} is not changed in any
450case; instead, the only effect of @code{defvar} is to declare locally that this
451variable exists elsewhere and should hence always use dynamic scoping rules.
448 452
449If @var{symbol} has a buffer-local binding in the current buffer, 453If @var{symbol} has a buffer-local binding in the current buffer,
450@code{defvar} operates on the default value, which is buffer-independent, 454@code{defvar} operates on the default value, which is buffer-independent,
@@ -881,7 +885,7 @@ the others.
881@cindex extent 885@cindex extent
882@cindex dynamic scoping 886@cindex dynamic scoping
883@cindex lexical scoping 887@cindex lexical scoping
884 Local bindings in Emacs Lisp have @dfn{indefinite scope} and 888 By default, local bindings in Emacs Lisp have @dfn{indefinite scope} and
885@dfn{dynamic extent}. @dfn{Scope} refers to @emph{where} textually in 889@dfn{dynamic extent}. @dfn{Scope} refers to @emph{where} textually in
886the source code the binding can be accessed. ``Indefinite scope'' means 890the source code the binding can be accessed. ``Indefinite scope'' means
887that any part of the program can potentially access the variable 891that any part of the program can potentially access the variable
@@ -893,6 +897,8 @@ lasts as long as the activation of the construct that established it.
893@dfn{dynamic scoping}. By contrast, most programming languages use 897@dfn{dynamic scoping}. By contrast, most programming languages use
894@dfn{lexical scoping}, in which references to a local variable must be 898@dfn{lexical scoping}, in which references to a local variable must be
895located textually within the function or block that binds the variable. 899located textually within the function or block that binds the variable.
900Emacs can also support lexical scoping, upon request (@pxref{Lexical
901Binding}).
896 902
897@cindex CL note---special variables 903@cindex CL note---special variables
898@quotation 904@quotation
@@ -901,11 +907,12 @@ dynamically scoped, like all variables in Emacs Lisp.
901@end quotation 907@end quotation
902 908
903@menu 909@menu
904* Scope:: Scope means where in the program a value is visible. 910* Scope:: Scope means where in the program a value is visible.
905 Comparison with other languages. 911 Comparison with other languages.
906* Extent:: Extent means how long in time a value exists. 912* Extent:: Extent means how long in time a value exists.
907* Impl of Scope:: Two ways to implement dynamic scoping. 913* Impl of Scope:: Two ways to implement dynamic scoping.
908* Using Scoping:: How to use dynamic scoping carefully and avoid problems. 914* Using Scoping:: How to use dynamic scoping carefully and avoid problems.
915* Lexical Binding::
909@end menu 916@end menu
910 917
911@node Scope 918@node Scope
@@ -969,12 +976,12 @@ Here, when @code{foo} is called by @code{binder}, it binds @code{x}.
969by @code{foo} instead of the one bound by @code{binder}. 976by @code{foo} instead of the one bound by @code{binder}.
970@end itemize 977@end itemize
971 978
972Emacs Lisp uses dynamic scoping because simple implementations of 979Emacs Lisp used dynamic scoping by default because simple implementations of
973lexical scoping are slow. In addition, every Lisp system needs to offer 980lexical scoping are slow. In addition, every Lisp system needs to offer
974dynamic scoping at least as an option; if lexical scoping is the norm, 981dynamic scoping at least as an option; if lexical scoping is the norm, there
975there must be a way to specify dynamic scoping instead for a particular 982must be a way to specify dynamic scoping instead for a particular variable.
976variable. It might not be a bad thing for Emacs to offer both, but 983Nowadays, Emacs offers both, but the default is still to use exclusively
977implementing it with dynamic scoping only was much easier. 984dynamic scoping.
978 985
979@node Extent 986@node Extent
980@subsection Extent 987@subsection Extent
@@ -1088,6 +1095,48 @@ for inter-function usage. It also avoids a warning from the byte
1088compiler. Choose the variable's name to avoid name conflicts---don't 1095compiler. Choose the variable's name to avoid name conflicts---don't
1089use short names like @code{x}. 1096use short names like @code{x}.
1090 1097
1098
1099@node Lexical Binding
1100@subsection Use of Lexical Scoping
1101
1102Emacs Lisp can be evaluated in two different modes: in dynamic binding mode or
1103lexical binding mode. In dynamic binding mode, all local variables use dynamic
1104scoping, whereas in lexical binding mode variables that have been declared
1105@dfn{special} (i.e., declared with @code{defvar} or @code{defconst}) use
1106dynamic scoping and all others use lexical scoping.
1107
1108@defvar lexical-binding
1109When non-nil, evaluation of Lisp code uses lexical scoping for non-special
1110local variables instead of dynamic scoping. If nil, dynamic scoping is used
1111for all local variables. This variable is typically set for a whole Elisp file
1112via file local variables (@pxref{File Local Variables}).
1113@end defvar
1114
1115@defun special-variable-p SYMBOL
1116Return whether SYMBOL has been declared as a special variable, via
1117@code{defvar} or @code{defconst}.
1118@end defun
1119
1120The use of a special variable as a formal argument in a function is generally
1121discouraged and its behavior in lexical binding mode is unspecified (it may use
1122lexical scoping sometimes and dynamic scoping other times).
1123
1124Functions like @code{symbol-value}, @code{boundp}, or @code{set} only know
1125about dynamically scoped variables, so you cannot get the value of a lexical
1126variable via @code{symbol-value} and neither can you change it via @code{set}.
1127Another particularity is that code in the body of a @code{defun} or
1128@code{defmacro} cannot refer to surrounding lexical variables.
1129
1130Evaluation of a @code{lambda} expression in lexical binding mode will not just
1131return that lambda expression unchanged, as in the dynamic binding case, but
1132will instead construct a new object that remembers the current lexical
1133environment in which that lambda expression was defined, so that the function
1134body can later be evaluated in the proper context. Those objects are called
1135@dfn{closures}. They are also functions, in the sense that they are accepted
1136by @code{funcall}, and they are represented by a cons cell whose @code{car} is
1137the symbol @code{closure}.
1138
1139
1091@node Buffer-Local Variables 1140@node Buffer-Local Variables
1092@section Buffer-Local Variables 1141@section Buffer-Local Variables
1093@cindex variable, buffer-local 1142@cindex variable, buffer-local
@@ -1103,9 +1152,9 @@ local to each terminal, or to each frame. @xref{Multiple Terminals},
1103and @xref{Frame-Local Variables}.) 1152and @xref{Frame-Local Variables}.)
1104 1153
1105@menu 1154@menu
1106* Intro to Buffer-Local:: Introduction and concepts. 1155* Intro to Buffer-Local:: Introduction and concepts.
1107* Creating Buffer-Local:: Creating and destroying buffer-local bindings. 1156* Creating Buffer-Local:: Creating and destroying buffer-local bindings.
1108* Default Value:: The default value is seen in buffers 1157* Default Value:: The default value is seen in buffers
1109 that don't have their own buffer-local values. 1158 that don't have their own buffer-local values.
1110@end menu 1159@end menu
1111 1160