Next: , Previous: , Up: Scoping Rules for Variable Bindings   [Contents][Index]

12.10.4 Selecting Lisp Dialect

When loading an Emacs Lisp file or evaluating a Lisp buffer, the Lisp dialect is selected using the buffer-local variable lexical-binding.

Variable: lexical-binding

If this buffer-local variable is non-nil, Emacs Lisp files and buffers are evaluated using the modern Lisp dialect that by default uses lexical binding instead of dynamic binding. If nil, the old dialect is used that uses dynamic binding for all local variables. This variable is typically set for a whole Emacs Lisp file, as a file-local variable (see File Local Variables). Note that unlike other such variables, this one must be set in the first line of a file.

In practice, dialect selection means that the first line in an Emacs Lisp file looks like:

;;; ...  -*- lexical-binding: t -*-

for the modern lexical-binding dialect, and

;;; ...  -*- lexical-binding: nil -*-

for the old dynamic-only dialect. When no declaration is present the old dialect is used, but this may change in a future release. The compiler will warn if no declaration is present.

When evaluating Emacs Lisp code directly using an eval call, lexical binding is enabled if the lexical argument to eval is non-nil. See Eval.

Lexical binding is also enabled in Lisp Interaction and IELM mode, used in the *scratch* and *ielm* buffers, and also when evaluating expressions via M-: (eval-expression) and when processing the --eval command-line options of Emacs (see Action Arguments in The GNU Emacs Manual) and emacsclient (see emacsclient Options in The GNU Emacs Manual).

Next: Converting to Lexical Binding, Previous: Proper Use of Dynamic Binding, Up: Scoping Rules for Variable Bindings   [Contents][Index]