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 save the buffer, which means copying the contents of the buffer into the file.
save-buffer
is responsible for making backup files. Normally,
backup-option is nil
, and save-buffer
makes a backup
file only if this is the first save since visiting the file. Other
values for backup-option request the making of backup files in
other circumstances:
save-buffer
function marks this version of the file to be
backed up when the buffer is next saved.
save-buffer
function unconditionally backs up the previous
version of the file before saving it.
nil
, it saves all the file-visiting buffers without querying
the user.
The optional exiting argument, if non-nil
, requests this
function to offer also to save certain other buffers that are not
visiting files. These are buffers that have a non-nil
buffer-local value of buffer-offer-save
. (A user who says yes to
saving one of these is asked to specify a file name to use.) The
save-buffers-kill-emacs
function passes a non-nil
value
for this argument.
set-visited-file-name
(see section Buffer File Name) and
save-buffer
.
Saving a buffer runs several hooks. It also performs format conversion (see section File Format Conversion), and may save text properties in "annotations" (see section Saving Text Properties in Files).
nil
, 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.
If a function in write-file-hooks
returns non-nil
, it
is responsible for making a backup file (if that is appropriate).
To do so, execute the following code:
(or buffer-backed-up (backup-buffer))
You might wish to save the file modes value returned by
backup-buffer
and use that to set the mode bits of the file that
you write. This is what save-buffer
normally does.
The hook functions in write-file-hooks
are also responsible for
encoding the data (if desired): they must choose a suitable coding
system (see section Coding Systems in Lisp), perform the encoding
(see section Explicit Encoding and Decoding), and set last-coding-system-used
to
the coding system that was used (see section Encoding and I/O).
Do not make this variable buffer-local. To set up buffer-specific hook
functions, use write-contents-hooks
instead.
Even though this is not a normal hook, you can use add-hook
and
remove-hook
to manipulate the list. See section Hooks.
write-file-hooks
, but it is intended to be
made buffer-local in particular buffers, and used for hooks that pertain
to the file name or the way the buffer contents were obtained.
The variable is marked as a permanent local, so that changing the major mode does not alter a buffer-local value. This is convenient for packages that read "file" contents in special ways, and set up hooks to save the data in a corresponding way.
write-file-hooks
, but it is intended for
hooks that pertain to the contents of the file, as opposed to hooks that
pertain to where the file came from. 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. When you use
add-hooks
to add an element to this hook, you should not
specify a non-nil
local argument, since this variable is
used only buffer-locally.
nil
, then save-buffer
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.
As a side effect, backups are necessarily made by copying. See section Backup by Renaming or by Copying?. Yet, at the same time, saving a precious file always breaks all hard links between the file you save and other file names.
Some modes give this variable a non-nil
buffer-local value
in particular buffers.
t
, then save-buffer
silently adds a newline at the end of
the file whenever the buffer being saved does not already end in one.
If the value of the variable is non-nil
, but not t
, then
save-buffer
asks the user whether to add a newline each time the
case arises.
If the value of the variable is nil
, then save-buffer
doesn't add newlines at all. nil
is the default value, but a few
major modes set it to t
in particular buffers.
See also the function set-visited-file-name
(see section Buffer File Name).
Go to the first, previous, next, last section, table of contents.