When you create a local binding for a variable, that binding takes effect only within a limited portion of the program (see Local Variables). This section describes exactly what this means.
Each local binding has a certain scope and extent. Scope refers to where in the textual source code the binding can be accessed. Extent refers to when, as the program is executing, the binding exists.
By default, the local bindings that Emacs creates are dynamic
bindings. Such a binding has dynamic scope, meaning that any
part of the program can potentially access the variable binding. It
also has dynamic extent, meaning that the binding lasts only
while the binding construct (such as the body of a
let form) is
Emacs can optionally create lexical bindings. A lexical binding has lexical scope, meaning that any reference to the variable must be located textually within the binding construct10. It also has indefinite extent, meaning that under some circumstances the binding can live on even after the binding construct has finished executing, by means of special objects called closures.
The dynamic binding was (and still is) the default in Emacs for many years, but lately Emacs is moving towards using lexical binding in more and more places, with the goal of eventually making that the default.
The following subsections describe dynamic binding and lexical binding in greater detail, and how to enable lexical binding in Emacs Lisp programs.
With some exceptions; for instance, a lexical binding can also be accessed from the Lisp debugger.