summaryrefslogtreecommitdiff
path: root/devdocs/elisp/dynamic-binding.html
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
committerCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
commit754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch)
treef1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/elisp/dynamic-binding.html
new repository
Diffstat (limited to 'devdocs/elisp/dynamic-binding.html')
-rw-r--r--devdocs/elisp/dynamic-binding.html37
1 files changed, 37 insertions, 0 deletions
diff --git a/devdocs/elisp/dynamic-binding.html b/devdocs/elisp/dynamic-binding.html
new file mode 100644
index 00000000..a34a0b35
--- /dev/null
+++ b/devdocs/elisp/dynamic-binding.html
@@ -0,0 +1,37 @@
+ <h4 class="subsection">Dynamic Binding</h4> <p>By default, the local variable bindings made by Emacs are dynamic bindings. When a variable is dynamically bound, its current binding at any point in the execution of the Lisp program is simply the most recently-created dynamic local binding for that symbol, or the global binding if there is no such local binding. </p> <p>Dynamic bindings have dynamic scope and extent, as shown by the following example: </p> <div class="example"> <pre class="example">(defvar x -99) ; <span class="roman"><code>x</code> receives an initial value of -99.</span>
+
+(defun getx ()
+ x) ; <span class="roman"><code>x</code> is used free in this function.</span>
+
+(let ((x 1)) ; <span class="roman"><code>x</code> is dynamically bound.</span>
+ (getx))
+ ⇒ 1
+
+;; <span class="roman">After the <code>let</code> form finishes, <code>x</code> reverts to its</span>
+;; <span class="roman">previous value, which is -99.</span>
+
+(getx)
+ ⇒ -99
+</pre>
+</div> <p>The function <code>getx</code> refers to <code>x</code>. This is a <em>free</em> reference, in the sense that there is no binding for <code>x</code> within that <code>defun</code> construct itself. When we call <code>getx</code> from within a <code>let</code> form in which <code>x</code> is (dynamically) bound, it retrieves the local value (i.e., 1). But when we call <code>getx</code> outside the <code>let</code> form, it retrieves the global value (i.e., -99). </p> <p>Here is another example, which illustrates setting a dynamically bound variable using <code>setq</code>: </p> <div class="example"> <pre class="example">(defvar x -99) ; <span class="roman"><code>x</code> receives an initial value of -99.</span>
+
+(defun addx ()
+ (setq x (1+ x))) ; <span class="roman">Add 1 to <code>x</code> and return its new value.</span>
+
+(let ((x 1))
+ (addx)
+ (addx))
+ ⇒ 3 ; <span class="roman">The two <code>addx</code> calls add to <code>x</code> twice.</span>
+
+;; <span class="roman">After the <code>let</code> form finishes, <code>x</code> reverts to its</span>
+;; <span class="roman">previous value, which is -99.</span>
+
+(addx)
+ ⇒ -98
+</pre>
+</div> <p>Dynamic binding is implemented in Emacs Lisp in a simple way. Each symbol has a value cell, which specifies its current dynamic value (or absence of value). See <a href="symbol-components">Symbol Components</a>. When a symbol is given a dynamic local binding, Emacs records the contents of the value cell (or absence thereof) in a stack, and stores the new local value in the value cell. When the binding construct finishes executing, Emacs pops the old value off the stack, and puts it in the value cell. </p> <p>Note that when code using Dynamic Binding is native compiled the native compiler will not perform any Lisp specific optimization. </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/Dynamic-Binding.html" class="_attribution-link">https://www.gnu.org/software/emacs/manual/html_node/elisp/Dynamic-Binding.html</a>
+ </p>
+</div>