summaryrefslogtreecommitdiff
path: root/devdocs/elisp/setting-variables.html
diff options
context:
space:
mode:
Diffstat (limited to 'devdocs/elisp/setting-variables.html')
-rw-r--r--devdocs/elisp/setting-variables.html55
1 files changed, 55 insertions, 0 deletions
diff --git a/devdocs/elisp/setting-variables.html b/devdocs/elisp/setting-variables.html
new file mode 100644
index 00000000..e44f94d9
--- /dev/null
+++ b/devdocs/elisp/setting-variables.html
@@ -0,0 +1,55 @@
+ <h3 class="section">Setting Variable Values</h3> <p>The usual way to change the value of a variable is with the special form <code>setq</code>. When you need to compute the choice of variable at run time, use the function <code>set</code>. </p> <dl> <dt id="setq">Special Form: <strong>setq</strong> <em>[symbol form]…</em>
+</dt> <dd>
+<p>This special form is the most common method of changing a variable’s value. Each <var>symbol</var> is given a new value, which is the result of evaluating the corresponding <var>form</var>. The current binding of the symbol is changed. </p> <p><code>setq</code> does not evaluate <var>symbol</var>; it sets the symbol that you write. We say that this argument is <em>automatically quoted</em>. The ‘<samp>q</samp>’ in <code>setq</code> stands for “quoted”. </p> <p>The value of the <code>setq</code> form is the value of the last <var>form</var>. </p> <div class="example"> <pre class="example">(setq x (1+ 2))
+ ⇒ 3
+</pre>
+<pre class="example">x ; <span class="roman"><code>x</code> now has a global value.</span>
+ ⇒ 3
+</pre>
+<pre class="example">(let ((x 5))
+ (setq x 6) ; <span class="roman">The local binding of <code>x</code> is set.</span>
+ x)
+ ⇒ 6
+</pre>
+<pre class="example">x ; <span class="roman">The global value is unchanged.</span>
+ ⇒ 3
+</pre>
+</div> <p>Note that the first <var>form</var> is evaluated, then the first <var>symbol</var> is set, then the second <var>form</var> is evaluated, then the second <var>symbol</var> is set, and so on: </p> <div class="example"> <pre class="example">(setq x 10 ; <span class="roman">Notice that <code>x</code> is set before</span>
+ y (1+ x)) ; <span class="roman">the value of <code>y</code> is computed.</span>
+ ⇒ 11
+</pre>
+</div> </dd>
+</dl> <dl> <dt id="set">Function: <strong>set</strong> <em>symbol value</em>
+</dt> <dd>
+<p>This function puts <var>value</var> in the value cell of <var>symbol</var>. Since it is a function rather than a special form, the expression written for <var>symbol</var> is evaluated to obtain the symbol to set. The return value is <var>value</var>. </p> <p>When dynamic variable binding is in effect (the default), <code>set</code> has the same effect as <code>setq</code>, apart from the fact that <code>set</code> evaluates its <var>symbol</var> argument whereas <code>setq</code> does not. But when a variable is lexically bound, <code>set</code> affects its <em>dynamic</em> value, whereas <code>setq</code> affects its current (lexical) value. See <a href="variable-scoping">Variable Scoping</a>. </p> <div class="example"> <pre class="example">(set one 1)
+error→ Symbol's value as variable is void: one
+</pre>
+<pre class="example">(set 'one 1)
+ ⇒ 1
+</pre>
+<pre class="example">(set 'two 'one)
+ ⇒ one
+</pre>
+<pre class="example">(set two 2) ; <span class="roman"><code>two</code> evaluates to symbol <code>one</code>.</span>
+ ⇒ 2
+</pre>
+<pre class="example">one ; <span class="roman">So it is <code>one</code> that was set.</span>
+ ⇒ 2
+(let ((one 1)) ; <span class="roman">This binding of <code>one</code> is set,</span>
+ (set 'one 3) ; <span class="roman">not the global value.</span>
+ one)
+ ⇒ 3
+</pre>
+<pre class="example">one
+ ⇒ 2
+</pre>
+</div> <p>If <var>symbol</var> is not actually a symbol, a <code>wrong-type-argument</code> error is signaled. </p> <div class="example"> <pre class="example">(set '(x y) 'z)
+error→ Wrong type argument: symbolp, (x y)
+</pre>
+</div> </dd>
+</dl><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/Setting-Variables.html" class="_attribution-link">https://www.gnu.org/software/emacs/manual/html_node/elisp/Setting-Variables.html</a>
+ </p>
+</div>