aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--doc/lispintro/emacs-lisp-intro.texi171
1 files changed, 149 insertions, 22 deletions
diff --git a/doc/lispintro/emacs-lisp-intro.texi b/doc/lispintro/emacs-lisp-intro.texi
index 1e10f62104a..b3fe8ce4589 100644
--- a/doc/lispintro/emacs-lisp-intro.texi
+++ b/doc/lispintro/emacs-lisp-intro.texi
@@ -3556,6 +3556,7 @@ and the two are not intended to refer to the same value. The
3556* Parts of let Expression:: 3556* Parts of let Expression::
3557* Sample let Expression:: 3557* Sample let Expression::
3558* Uninitialized let Variables:: 3558* Uninitialized let Variables::
3559* How let Binds Variables::
3559@end menu 3560@end menu
3560 3561
3561@ifnottex 3562@ifnottex
@@ -3569,24 +3570,26 @@ and the two are not intended to refer to the same value. The
3569@cindex @samp{variable, local}, defined 3570@cindex @samp{variable, local}, defined
3570The @code{let} special form prevents confusion. @code{let} creates a 3571The @code{let} special form prevents confusion. @code{let} creates a
3571name for a @dfn{local variable} that overshadows any use of the same 3572name for a @dfn{local variable} that overshadows any use of the same
3572name outside the @code{let} expression. This is like understanding 3573name outside the @code{let} expression (in computer science jargon, we
3573that whenever your host refers to ``the house'', he means his house, not 3574call this @dfn{binding} the variable). This is like understanding
3574yours. (Symbols used in argument lists work the same way. 3575that in your host's home, whenever he refers to ``the house'', he
3576means his house, not yours. (The symbols used to name function
3577arguments are bound as local variables in exactly the same way.
3575@xref{defun, , The @code{defun} Macro}.) 3578@xref{defun, , The @code{defun} Macro}.)
3576 3579
3577Local variables created by a @code{let} expression retain their value 3580Another way to think about @code{let} is that it defines a special
3578@emph{only} within the @code{let} expression itself (and within 3581region in your code: within the body of the @code{let} expression, the
3579expressions called within the @code{let} expression); the local 3582variables you've named have their own local meaning. Outside of the
3580variables have no effect outside the @code{let} expression. 3583@code{let} body, they have other meanings (or they may not be defined
3581 3584at all). This means that inside the @code{let} body, calling
3582Another way to think about @code{let} is that it is like a @code{setq} 3585@code{setq} for a variable named by the @code{let} expression will set
3583that is temporary and local. The values set by @code{let} are 3586the value of the @emph{local} variable of that name. However, outside
3584automatically undone when the @code{let} is finished. The setting 3587of the @code{let} body (such as when calling a function that was
3585only affects expressions that are inside the bounds of the @code{let} 3588defined elsewhere), calling @code{setq} for a variable named by the
3586expression. In computer science jargon, we would say the binding of 3589@code{let} expression will @emph{not} affect that local
3587a symbol is visible only in functions called in the @code{let} form; 3590variable.@footnote{This describes the behavior of @code{let} when
3588in Emacs Lisp, the default scoping is dynamic, not lexical. (The 3591using a style called ``lexical binding'' (@pxref{How let Binds
3589non-default lexical binding is not discussed in this manual.) 3592Variables}).}
3590 3593
3591@code{let} can create more than one variable at once. Also, 3594@code{let} can create more than one variable at once. Also,
3592@code{let} gives each variable it creates an initial value, either a 3595@code{let} gives each variable it creates an initial value, either a
@@ -3746,6 +3749,128 @@ number is printed in the message using a @samp{%d} rather than a
3746@samp{%s}.) The four variables as a group are put into a list to 3749@samp{%s}.) The four variables as a group are put into a list to
3747delimit them from the body of the @code{let}. 3750delimit them from the body of the @code{let}.
3748 3751
3752@node How let Binds Variables
3753@subsection How @code{let} Binds Variables
3754
3755Emacs Lisp supports two different ways of binding variable names to
3756their values. These ways affect the parts of your program where a
3757particular binding is valid. For historical reasons, Emacs Lisp uses
3758a form of variable binding called @dfn{dynamic binding} by default.
3759However, in this manual we discuss the preferred form of binding,
3760called @dfn{lexical binding}, unless otherwise noted (in the future,
3761the Emacs maintainers plan to change the default to lexical binding).
3762If you have programmed in other languages before, you're likely
3763already familiar with how lexical binding behaves.
3764
3765In order to use lexical binding in a program, you should add this to
3766the first line of your Emacs Lisp file:
3767
3768@example
3769;;; -*- lexical-binding: t -*-
3770@end example
3771
3772For more information about this, @pxref{Selecting Lisp Dialect, , ,
3773elisp, The Emacs Lisp Reference Manual}.
3774
3775@menu
3776* Lexical & Dynamic Binding Differences::
3777* Lexical vs. Dynamic Binding Example::
3778@end menu
3779
3780@node Lexical & Dynamic Binding Differences
3781@unnumberedsubsubsec Differences Between Lexical and Dynamic Binding
3782
3783@cindex Lexical binding
3784@cindex Binding, lexical
3785As we discussed before (@pxref{Prevent confusion}), when you create
3786local variables with @code{let} under lexical binding, those variables
3787are valid only within the body of the @code{let} expression. In other
3788parts of your code, they have other meanings, so if you call a
3789function defined elsewhere within the @code{let} body, that function
3790would be unable to ``see'' the local variables you've created. (On
3791the other hand, if you call a function that was defined within a
3792@code{let} body, that function @emph{would} be able to see---and
3793modify---the local variables from that @code{let} expression.)
3794
3795@cindex Dynamic binding
3796@cindex Binding, dynamic
3797Under dynamic binding, the rules are different: instead, when you use
3798@code{let}, the local variables you've created are valid during
3799execution of the @code{let} expression. This means that, if your
3800@code{let} expression calls a function, that function can see these
3801local variables, regardless of where the function is defined
3802(including in another file entirely).
3803
3804Another way to think about @code{let} when using dynamic binding is
3805that every variable name has a global ``stack'' of bindings, and
3806whenever you use that variable's name, it refers to the binding on the
3807top of the stack. (You can imagine this like a stack of papers on
3808your desk with the values written on them.) When you bind a variable
3809dynamically with @code{let}, it puts the new binding you've specified
3810on the top of the stack, and then executes the @code{let} body. Once
3811the @code{let} body finishes, it takes that binding off of the stack,
3812revealing the one it had (if any) before the @code{let} expression.
3813
3814@node Lexical vs. Dynamic Binding Example
3815@unnumberedsubsubsec Example of Lexical vs. Dynamic Binding
3816In some cases, both lexical and dynamic binding behave identically.
3817However, in other cases, they can change the meaning of your program.
3818For example, see what happens in this code under lexical binding:
3819
3820@example
3821;;; -*- lexical-binding: t -*-
3822
3823(setq x 0)
3824
3825(defun getx ()
3826 x)
3827
3828(setq x 1)
3829
3830(let ((x 2))
3831 (getx))
3832 @result{} 1
3833@end example
3834
3835@noindent
3836Here, the result of @code{(getx)} is @code{1}. Under lexical binding,
3837@code{getx} doesn't see the value from our @code{let} expression.
3838That's because the body of @code{getx} is outside of the body of our
3839@code{let} expression. Since @code{getx} is defined at the top,
3840global level of our code (i.e.@: not inside the body of any @code{let}
3841expression), it looks for and finds @code{x} at the global level as
3842well. When executing @code{getx}, the current global value of
3843@code{x} is @code{1}, so that's what @code{getx} returns.
3844
3845If we use dynamic binding instead, the behavior is different:
3846
3847@example
3848;;; -*- lexical-binding: nil -*-
3849
3850(setq x 0)
3851
3852(defun getx ()
3853 x)
3854
3855(setq x 1)
3856
3857(let ((x 2))
3858 (getx))
3859 @result{} 2
3860@end example
3861
3862@noindent
3863Now, the result of @code{(getx)} is @code{2}! That's because under
3864dynamic binding, when executing @code{getx}, the current binding for
3865@code{x} at the top of our stack is the one from our @code{let}
3866binding. This time, @code{getx} doesn't see the global value for
3867@code{x}, since its binding is below the one from our @code{let}
3868expression in the stack of bindings.
3869
3870(Some variables are also ``special'', and they are always dynamically
3871bound even when @code{lexical-binding} is @code{t}. @xref{defvar, ,
3872Initializing a Variable with @code{defvar}}.)
3873
3749@node if 3874@node if
3750@section The @code{if} Special Form 3875@section The @code{if} Special Form
3751@findex if 3876@findex if
@@ -9101,12 +9226,14 @@ In Emacs Lisp, a variable such as the @code{kill-ring} is created and
9101given an initial value by using the @code{defvar} special form. The 9226given an initial value by using the @code{defvar} special form. The
9102name comes from ``define variable''. 9227name comes from ``define variable''.
9103 9228
9104The @code{defvar} special form is similar to @code{setq} in that it sets 9229The @code{defvar} special form is similar to @code{setq} in that it
9105the value of a variable. It is unlike @code{setq} in two ways: first, 9230sets the value of a variable. It is unlike @code{setq} in three ways:
9106it only sets the value of the variable if the variable does not already 9231first, it marks the variable as ``special'' so that it is always
9107have a value. If the variable already has a value, @code{defvar} does 9232dynamically bound, even when @code{lexical-binding} is @code{t}
9108not override the existing value. Second, @code{defvar} has a 9233(@pxref{How let Binds Variables}). Second, it only sets the value of
9109documentation string. 9234the variable if the variable does not already have a value. If the
9235variable already has a value, @code{defvar} does not override the
9236existing value. Third, @code{defvar} has a documentation string.
9110 9237
9111(There is a related macro, @code{defcustom}, designed for variables 9238(There is a related macro, @code{defcustom}, designed for variables
9112that people customize. It has more features than @code{defvar}. 9239that people customize. It has more features than @code{defvar}.