summaryrefslogtreecommitdiff
path: root/devdocs/elisp/saving-buffers.html
diff options
context:
space:
mode:
Diffstat (limited to 'devdocs/elisp/saving-buffers.html')
-rw-r--r--devdocs/elisp/saving-buffers.html42
1 files changed, 42 insertions, 0 deletions
diff --git a/devdocs/elisp/saving-buffers.html b/devdocs/elisp/saving-buffers.html
new file mode 100644
index 00000000..8fba541e
--- /dev/null
+++ b/devdocs/elisp/saving-buffers.html
@@ -0,0 +1,42 @@
+ <h3 class="section">Saving Buffers</h3> <p>When you edit a file in Emacs, you are actually working on a buffer that is visiting that file—that is, the contents of the file are copied into the buffer and the copy is what you edit. Changes to the buffer do not change the file until you <em>save</em> the buffer, which means copying the contents of the buffer into the file. Buffers which are not visiting a file can still be “saved”, in a sense, using functions in the buffer-local <code>write-contents-functions</code> hook. </p> <dl> <dt id="save-buffer">Command: <strong>save-buffer</strong> <em>&amp;optional backup-option</em>
+</dt> <dd>
+<p>This function saves the contents of the current buffer in its visited file if the buffer has been modified since it was last visited or saved. Otherwise it does nothing. </p> <p><code>save-buffer</code> is responsible for making backup files. Normally, <var>backup-option</var> is <code>nil</code>, and <code>save-buffer</code> makes a backup file only if this is the first save since visiting the file. Other values for <var>backup-option</var> request the making of backup files in other circumstances: </p> <ul> <li> With an argument of 4 or 64, reflecting 1 or 3 <kbd>C-u</kbd>’s, the <code>save-buffer</code> function marks this version of the file to be backed up when the buffer is next saved. </li>
+<li> With an argument of 16 or 64, reflecting 2 or 3 <kbd>C-u</kbd>’s, the <code>save-buffer</code> function unconditionally backs up the previous version of the file before saving it. </li>
+<li> With an argument of 0, unconditionally do <em>not</em> make any backup file. </li>
+</ul> </dd>
+</dl> <dl> <dt id="save-some-buffers">Command: <strong>save-some-buffers</strong> <em>&amp;optional save-silently-p pred</em>
+</dt> <dd>
+<p>This command saves some modified file-visiting buffers. Normally it asks the user about each buffer. But if <var>save-silently-p</var> is non-<code>nil</code>, it saves all the file-visiting buffers without querying the user. </p> <p>The optional <var>pred</var> argument provides a predicate that controls which buffers to ask about (or to save silently if <var>save-silently-p</var> is non-<code>nil</code>). If <var>pred</var> is <code>nil</code>, that means to use the value of <code>save-some-buffers-default-predicate</code> instead of <var>pred</var>. If the result is <code>nil</code>, it means ask only about file-visiting buffers. If it is <code>t</code>, that means also offer to save certain other non-file buffers—those that have a non-<code>nil</code> buffer-local value of <code>buffer-offer-save</code> (see <a href="killing-buffers">Killing Buffers</a>). A user who says ‘<samp>yes</samp>’ to saving a non-file buffer is asked to specify the file name to use. The <code>save-buffers-kill-emacs</code> function passes the value <code>t</code> for <var>pred</var>. </p> <p>If the predicate is neither <code>t</code> nor <code>nil</code>, then it should be a function of no arguments. It will be called in each buffer to decide whether to offer to save that buffer. If it returns a non-<code>nil</code> value in a certain buffer, that means do offer to save that buffer. </p>
+</dd>
+</dl> <dl> <dt id="write-file">Command: <strong>write-file</strong> <em>filename &amp;optional confirm</em>
+</dt> <dd>
+<p>This function writes the current buffer into file <var>filename</var>, makes the buffer visit that file, and marks it not modified. Then it renames the buffer based on <var>filename</var>, appending a string like ‘<samp>&lt;2&gt;</samp>’ if necessary to make a unique buffer name. It does most of this work by calling <code>set-visited-file-name</code> (see <a href="buffer-file-name">Buffer File Name</a>) and <code>save-buffer</code>. </p> <p>If <var>confirm</var> is non-<code>nil</code>, that means to ask for confirmation before overwriting an existing file. Interactively, confirmation is required, unless the user supplies a prefix argument. </p> <p>If <var>filename</var> is a directory name (see <a href="directory-names">Directory Names</a>), <code>write-file</code> uses the name of the visited file, in directory <var>filename</var>. If the buffer is not visiting a file, it uses the buffer name instead. </p>
+</dd>
+</dl> <p>Saving a buffer runs several hooks. It also performs format conversion (see <a href="format-conversion">Format Conversion</a>). Note that these hooks, described below, are only run by <code>save-buffer</code>, they are not run by other primitives and functions that write buffer text to files, and in particular auto-saving (see <a href="auto_002dsaving">Auto-Saving</a>) doesn’t run these hooks. </p> <dl> <dt id="write-file-functions">Variable: <strong>write-file-functions</strong>
+</dt> <dd>
+<p>The value of this variable is a list of functions to be called before writing out a buffer to its visited file. If one of them returns non-<code>nil</code>, the file is considered already written and the rest of the functions are not called, nor is the usual code for writing the file executed. </p> <p>If a function in <code>write-file-functions</code> returns non-<code>nil</code>, it is responsible for making a backup file (if that is appropriate). To do so, execute the following code: </p> <div class="example"> <pre class="example">(or buffer-backed-up (backup-buffer))
+</pre>
+</div> <p>You might wish to save the file modes value returned by <code>backup-buffer</code> and use that (if non-<code>nil</code>) to set the mode bits of the file that you write. This is what <code>save-buffer</code> normally does. See <a href="making-backups">Making Backup Files</a>. </p> <p>The hook functions in <code>write-file-functions</code> are also responsible for encoding the data (if desired): they must choose a suitable coding system and end-of-line conversion (see <a href="lisp-and-coding-systems">Lisp and Coding Systems</a>), perform the encoding (see <a href="explicit-encoding">Explicit Encoding</a>), and set <code>last-coding-system-used</code> to the coding system that was used (see <a href="encoding-and-i_002fo">Encoding and I/O</a>). </p> <p>If you set this hook locally in a buffer, it is assumed to be associated with the file or the way the contents of the buffer were obtained. Thus the variable is marked as a permanent local, so that changing the major mode does not alter a buffer-local value. On the other hand, calling <code>set-visited-file-name</code> will reset it. If this is not what you want, you might like to use <code>write-contents-functions</code> instead. </p> <p>Even though this is not a normal hook, you can use <code>add-hook</code> and <code>remove-hook</code> to manipulate the list. See <a href="hooks">Hooks</a>. </p>
+</dd>
+</dl> <dl> <dt id="write-contents-functions">Variable: <strong>write-contents-functions</strong>
+</dt> <dd>
+<p>This works just like <code>write-file-functions</code>, but it is intended for hooks that pertain to the buffer’s contents, not to the particular visited file or its location, and can be used to create arbitrary save processes for buffers that aren’t visiting files at all. Such hooks are usually set up by major modes, as buffer-local bindings for this variable. This variable automatically becomes buffer-local whenever it is set; switching to a new major mode always resets this variable, but calling <code>set-visited-file-name</code> does not. </p> <p>If any of the functions in this hook returns non-<code>nil</code>, the file is considered already written and the rest are not called and neither are the functions in <code>write-file-functions</code>. </p> <p>When using this hook to save buffers that are not visiting files (for instance, special-mode buffers), keep in mind that, if the function fails to save correctly and returns a <code>nil</code> value, <code>save-buffer</code> will go on to prompt the user for a file to save the buffer in. If this is undesirable, consider having the function fail by raising an error. </p>
+</dd>
+</dl> <dl> <dt id="before-save-hook">User Option: <strong>before-save-hook</strong>
+</dt> <dd><p>This normal hook runs before a buffer is saved in its visited file, regardless of whether that is done normally or by one of the hooks described above. For instance, the <samp>copyright.el</samp> program uses this hook to make sure the file you are saving has the current year in its copyright notice. </p></dd>
+</dl> <dl> <dt id="after-save-hook">User Option: <strong>after-save-hook</strong>
+</dt> <dd><p>This normal hook runs after a buffer has been saved in its visited file. </p></dd>
+</dl> <dl> <dt id="file-precious-flag">User Option: <strong>file-precious-flag</strong>
+</dt> <dd>
+<p>If this variable is non-<code>nil</code>, then <code>save-buffer</code> protects against I/O errors while saving by writing the new file to a temporary name instead of the name it is supposed to have, and then renaming it to the intended name after it is clear there are no errors. This procedure prevents problems such as a lack of disk space from resulting in an invalid file. </p> <p>As a side effect, backups are necessarily made by copying. See <a href="rename-or-copy">Rename or Copy</a>. Yet, at the same time, saving a precious file always breaks all hard links between the file you save and other file names. </p> <p>Some modes give this variable a non-<code>nil</code> buffer-local value in particular buffers. </p>
+</dd>
+</dl> <dl> <dt id="require-final-newline">User Option: <strong>require-final-newline</strong>
+</dt> <dd>
+<p>This variable determines whether files may be written out that do <em>not</em> end with a newline. If the value of the variable is <code>t</code>, then <code>save-buffer</code> silently adds a newline at the end of the buffer whenever it does not already end in one. If the value is <code>visit</code>, Emacs adds a missing newline just after it visits the file. If the value is <code>visit-save</code>, Emacs adds a missing newline both on visiting and on saving. For any other non-<code>nil</code> value, <code>save-buffer</code> asks the user whether to add a newline each time the case arises. </p> <p>If the value of the variable is <code>nil</code>, then <code>save-buffer</code> doesn’t add newlines at all. <code>nil</code> is the default value, but a few major modes set it to <code>t</code> in particular buffers. </p>
+</dd>
+</dl> <p>See also the function <code>set-visited-file-name</code> (see <a href="buffer-file-name">Buffer File Name</a>). </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/Saving-Buffers.html" class="_attribution-link">https://www.gnu.org/software/emacs/manual/html_node/elisp/Saving-Buffers.html</a>
+ </p>
+</div>