summaryrefslogtreecommitdiff
path: root/devdocs/elisp/using-lexical-binding.html
diff options
context:
space:
mode:
Diffstat (limited to 'devdocs/elisp/using-lexical-binding.html')
-rw-r--r--devdocs/elisp/using-lexical-binding.html29
1 files changed, 29 insertions, 0 deletions
diff --git a/devdocs/elisp/using-lexical-binding.html b/devdocs/elisp/using-lexical-binding.html
new file mode 100644
index 00000000..3607b0f3
--- /dev/null
+++ b/devdocs/elisp/using-lexical-binding.html
@@ -0,0 +1,29 @@
+ <h4 class="subsection">Using Lexical Binding</h4> <p>When loading an Emacs Lisp file or evaluating a Lisp buffer, lexical binding is enabled if the buffer-local variable <code>lexical-binding</code> is non-<code>nil</code>: </p> <dl> <dt id="lexical-binding">Variable: <strong>lexical-binding</strong>
+</dt> <dd><p>If this buffer-local variable is non-<code>nil</code>, Emacs Lisp files and buffers are evaluated using lexical binding instead of dynamic binding. (However, special variables are still dynamically bound; see below.) If <code>nil</code>, dynamic binding is used for all local variables. This variable is typically set for a whole Emacs Lisp file, as a file local variable (see <a href="file-local-variables">File Local Variables</a>). Note that unlike other such variables, this one must be set in the first line of a file. </p></dd>
+</dl> <p>When evaluating Emacs Lisp code directly using an <code>eval</code> call, lexical binding is enabled if the <var>lexical</var> argument to <code>eval</code> is non-<code>nil</code>. See <a href="eval">Eval</a>. </p> <p>Lexical binding is also enabled in Lisp Interaction and IELM mode, used in the <samp>*scratch*</samp> and <samp>*ielm*</samp> buffers, and also when evaluating expressions via <kbd>M-:</kbd> (<code>eval-expression</code>) and when processing the <samp>--eval</samp> command-line options of Emacs (see <a href="https://www.gnu.org/software/emacs/manual/html_node/emacs/Action-Arguments.html#Action-Arguments">Action Arguments</a> in <cite>The GNU Emacs Manual</cite>) and <code>emacsclient</code> (see <a href="https://www.gnu.org/software/emacs/manual/html_node/emacs/emacsclient-Options.html#emacsclient-Options">emacsclient Options</a> in <cite>The GNU Emacs Manual</cite>). </p> <p>Even when lexical binding is enabled, certain variables will continue to be dynamically bound. These are called <em>special variables</em>. Every variable that has been defined with <code>defvar</code>, <code>defcustom</code> or <code>defconst</code> is a special variable (see <a href="defining-variables">Defining Variables</a>). All other variables are subject to lexical binding. </p> <p>Using <code>defvar</code> without a value, it is possible to bind a variable dynamically just in one file, or in just one part of a file while still binding it lexically elsewhere. For example: </p> <div class="example"> <pre class="example">(let (_)
+ (defvar x) ; <span class="roman">Let-bindings of <code>x</code> will be dynamic within this let.</span>
+ (let ((x -99)) ; <span class="roman">This is a dynamic binding of <code>x</code>.</span>
+ (defun get-dynamic-x ()
+ x)))
+
+(let ((x 'lexical)) ; <span class="roman">This is a lexical binding of <code>x</code>.</span>
+ (defun get-lexical-x ()
+ x))
+
+(let (_)
+ (defvar x)
+ (let ((x 'dynamic))
+ (list (get-lexical-x)
+ (get-dynamic-x))))
+ ⇒ (lexical dynamic)
+</pre>
+</div> <dl> <dt id="special-variable-p">Function: <strong>special-variable-p</strong> <em>symbol</em>
+</dt> <dd>
+<p>This function returns non-<code>nil</code> if <var>symbol</var> is a special variable (i.e., it has a <code>defvar</code>, <code>defcustom</code>, or <code>defconst</code> variable definition). Otherwise, the return value is <code>nil</code>. </p> <p>Note that since this is a function, it can only return non-<code>nil</code> for variables which are permanently special, but not for those that are only special in the current lexical scope. </p>
+</dd>
+</dl> <p>The use of a special variable as a formal argument in a function is not supported. </p><div class="_attribution">
+ <p class="_attribution-p">
+ Copyright &copy; 1990-1996, 1998-2022 Free Software Foundation, Inc. <br>Licensed under the GNU GPL license.<br>
+ <a href="https://www.gnu.org/software/emacs/manual/html_node/elisp/Using-Lexical-Binding.html" class="_attribution-link">https://www.gnu.org/software/emacs/manual/html_node/elisp/Using-Lexical-Binding.html</a>
+ </p>
+</div>