1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<h3 class="section">Excursions</h3> <p>It is often useful to move point temporarily within a localized portion of the program. This is called an <em>excursion</em>, and it is done with the <code>save-excursion</code> special form. This construct remembers the initial identity of the current buffer, and its value of point, and restores them after the excursion completes. It is the standard way to move point within one part of a program and avoid affecting the rest of the program, and is used thousands of times in the Lisp sources of Emacs. </p> <p>If you only need to save and restore the identity of the current buffer, use <code>save-current-buffer</code> or <code>with-current-buffer</code> instead (see <a href="current-buffer">Current Buffer</a>). If you need to save or restore window configurations, see the forms described in <a href="window-configurations">Window Configurations</a> and in <a href="frame-configurations">Frame Configurations</a>. </p> <dl> <dt id="save-excursion">Special Form: <strong>save-excursion</strong> <em>body…</em>
</dt> <dd>
<p>This special form saves the identity of the current buffer and the value of point in it, evaluates <var>body</var>, and finally restores the buffer and its saved value of point. Both saved values are restored even in case of an abnormal exit via <code>throw</code> or error (see <a href="nonlocal-exits">Nonlocal Exits</a>). </p> <p>The value returned by <code>save-excursion</code> is the result of the last form in <var>body</var>, or <code>nil</code> if no body forms were given. </p>
</dd>
</dl> <p>Because <code>save-excursion</code> only saves point for the buffer that was current at the start of the excursion, any changes made to point in other buffers, during the excursion, will remain in effect afterward. This frequently leads to unintended consequences, so the byte compiler warns if you call <code>set-buffer</code> during an excursion: </p> <div class="example"> <pre class="example">Warning: Use ‘with-current-buffer’ rather than
save-excursion+set-buffer
</pre>
</div> <p>To avoid such problems, you should call <code>save-excursion</code> only after setting the desired current buffer, as in the following example: </p> <div class="example"> <pre class="example">(defun append-string-to-buffer (string buffer)
"Append STRING to the end of BUFFER."
(with-current-buffer buffer
(save-excursion
(goto-char (point-max))
(insert string))))
</pre>
</div> <p>Likewise, <code>save-excursion</code> does not restore window-buffer correspondences altered by functions such as <code>switch-to-buffer</code>. </p> <p><strong>Warning:</strong> Ordinary insertion of text adjacent to the saved point value relocates the saved value, just as it relocates all markers. More precisely, the saved value is a marker with insertion type <code>nil</code>. See <a href="marker-insertion-types">Marker Insertion Types</a>. Therefore, when the saved point value is restored, it normally comes before the inserted text. </p> <dl> <dt id="save-mark-and-excursion">Macro: <strong>save-mark-and-excursion</strong> <em>body…</em>
</dt> <dd>
<p>This macro is like <code>save-excursion</code>, but also saves and restores the mark location and <code>mark-active</code>. This macro does what <code>save-excursion</code> did before Emacs 25.1. </p>
</dd>
</dl><div class="_attribution">
<p class="_attribution-p">
Copyright © 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/Excursions.html" class="_attribution-link">https://www.gnu.org/software/emacs/manual/html_node/elisp/Excursions.html</a>
</p>
</div>
|