Commit 3e01fd9d authored by Richard M. Stallman's avatar Richard M. Stallman
Browse files

Initial revision

parent 3ea1f391
@c -*-texinfo-*-
@c This is part of the GNU Emacs Lisp Reference Manual.
@c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
@c See the file elisp.texi for copying conditions.
@setfilename ../info/files
@node Files, Backups and Auto-Saving, Documentation, Top
@comment node-name, next, previous, up
@chapter Files
In Emacs, you can find, create, view, save, and otherwise work with
files and file directories. This chapter describes most of the
file-related functions of Emacs Lisp, but a few others are described in
@ref{Buffers}, and those related to backups and auto-saving are
described in @ref{Backups and Auto-Saving}.
* Visiting Files:: Reading files into Emacs buffers for editing.
* Saving Buffers:: Writing changed buffers back into files.
* Reading from Files:: Reading files into buffers without visiting.
* Writing to Files:: Writing new files from parts of buffers.
* File Locks:: Locking and unlocking files, to prevent
simultaneous editing by two people.
* Information about Files:: Testing existence, accessibility, size of files.
* Changing File Attributes:: Renaming files, changing protection, etc.
* File Names:: Decomposing and expanding file names.
* Contents of Directories:: Getting a list of the files in a directory.
* Create/Delete Dirs:: Creating and Deleting Directories.
* Magic File Names:: Defining "magic" special handling
for certain file names.
@end menu
@node Visiting Files
@section Visiting Files
@cindex finding files
@cindex visiting files
Visiting a file means reading a file into a buffer. Once this is
done, we say that the buffer is @dfn{visiting} that file, and call the
file ``the visited file'' of the buffer.
A file and a buffer are two different things. A file is information
recorded permanently in the computer (unless you delete it). A buffer,
on the other hand, is information inside of Emacs that will vanish at
the end of the editing session (or when you kill the buffer). Usually,
a buffer contains information that you have copied from a file; then we
say the buffer is visiting that file. The copy in the buffer is what
you modify with editing commands. Such changes to the buffer do not
change the file; therefore, to make the changes permanent, you must
@dfn{save} the buffer, which means copying the altered buffer contents
back into the file.
In spite of the distinction between files and buffers, people often
refer to a file when they mean a buffer and vice-versa. Indeed, we say,
``I am editing a file,'' rather than, ``I am editing a buffer which I
will soon save as a file of the same name.'' Humans do not usually need
to make the distinction explicit. When dealing with a computer program,
however, it is good to keep the distinction in mind.
* Visiting Functions:: The usual interface functions for visiting.
* Subroutines of Visiting:: Lower-level subroutines that they use.
@end menu
@node Visiting Functions
@subsection Functions for Visiting Files
This section describes the functions normally used to visit files.
For historical reasons, these functions have names starting with
@samp{find-} rather than @samp{visit-}. @xref{Buffer File Name}, for
functions and variables that access the visited file name of a buffer or
that find an existing buffer by its visited file name.
@deffn Command find-file filename
This command selects a buffer visiting the file @var{filename},
using an existing buffer if there is one, and otherwise creating a
new buffer and reading the file into it. It also returns that buffer.
The body of the @code{find-file} function is very simple and looks
like this:
(switch-to-buffer (find-file-noselect filename))
@end example
(See @code{switch-to-buffer} in @ref{Displaying Buffers}.)
When @code{find-file} is called interactively, it prompts for
@var{filename} in the minibuffer.
@end deffn
@defun find-file-noselect filename
This function is the guts of all the file-visiting functions. It finds
or creates a buffer visiting the file @var{filename}, and returns it.
It uses an existing buffer if there is one, and otherwise creates a new
buffer and reads the file into it. You may make the buffer current or
display it in a window if you wish, but this function does not do so.
When @code{find-file-noselect} uses an existing buffer, it first
verifies that the file has not changed since it was last visited or
saved in that buffer. If the file has changed, then this function asks
the user whether to reread the changed file. If the user says
@samp{yes}, any changes previously made in the buffer are lost.
If @code{find-file-noselect} needs to create a buffer, and there is no
file named @var{filename}, it displays the message @samp{New file} in
the echo area, and leaves the buffer empty.
The @code{find-file-noselect} function calls @code{after-find-file}
after reading the file (@pxref{Subroutines of Visiting}). That function
sets the buffer major mode, parses local variables, warns the user if
there exists an auto-save file more recent than the file just visited,
and finishes by running the functions in @code{find-file-hooks}.
The @code{find-file-noselect} function returns the buffer that is
visiting the file @var{filename}.
(find-file-noselect "/etc/fstab")
@result{} #<buffer fstab>
@end group
@end example
@end defun
@deffn Command find-alternate-file filename
This command selects a buffer visiting the file @var{filename}, then
kills the buffer that was previously displayed in the selected window.
It is useful if you have visited the wrong file by mistake, so that you
can get rid of the buffer that you did not want to create, at the same
time as you visit the file you intended.
When this command is called interactively, it prompts for @var{filename}.
@end deffn
@deffn Command find-file-other-window filename
This command selects a buffer visiting the file @var{filename}, but
does so in a window other than the selected window. It may use another
existing window or split a window; see @ref{Displaying Buffers}.
When this command is called interactively, it prompts for
@end deffn
@deffn Command find-file-read-only filename
This command selects a buffer visiting the file @var{filename}, like
@code{find-file}, but it marks the buffer as read-only. @xref{Read Only
Buffers}, for related functions and variables.
When this command is called interactively, it prompts for
@end deffn
@deffn Command view-file filename
This command views @var{filename} in View mode, returning to the
previous buffer when done. View mode is a mode that allows you to skim
rapidly through the file but does not let you modify it. Entering View
mode runs the normal hook @code{view-mode-hook}. @xref{Hooks}.
When @code{view-file} is called interactively, it prompts for
@end deffn
@defvar find-file-hooks
The value of this variable is a list of functions to be called after a
file is visited. The file's local-variables specification (if any) will
have been processed before the hooks are run. The buffer visiting the
file is current when the hook functions are run.
This variable works just like a normal hook, but we think that renaming
it would not be advisable.
@end defvar
@defvar find-file-not-found-hooks
The value of this variable is a list of functions to be called when
@code{find-file} or @code{find-file-noselect} is passed a nonexistent
file name. @code{find-file-noselect} calls these functions as soon as
it detects a nonexistent file. It calls them in the order of the list,
until one of them returns non-@code{nil}. @code{buffer-file-name} is
already set up.
This is not a normal hook because the values of the functions are
used and they may not all be called.
@end defvar
@node Subroutines of Visiting
@comment node-name, next, previous, up
@subsection Subroutines of Visiting
The @code{find-file-noselect} function uses the
@code{create-file-buffer} and @code{after-find-file} functions as
subroutines. Sometimes it is useful to call them directly.
@defun create-file-buffer filename
This function creates a suitably named buffer for visiting
@var{filename}, and returns it. It uses @var{filename} (sans directory)
as the name if that name is free; otherwise, it appends a string such as
@samp{<2>} to get an unused name. See also @ref{Creating Buffers}.
@strong{Please note:} @code{create-file-buffer} does @emph{not}
associate the new buffer with a file and does not select the buffer.
(create-file-buffer "foo")
@result{} #<buffer foo>
@end group
(create-file-buffer "foo")
@result{} #<buffer foo<2>>
@end group
(create-file-buffer "foo")
@result{} #<buffer foo<3>>
@end group
@end example
This function is used by @code{find-file-noselect}.
It uses @code{generate-new-buffer} (@pxref{Creating Buffers}).
@end defun
@defun after-find-file &optional error warn
This function sets the buffer major mode, and parses local variables
(@pxref{Auto Major Mode}). It is called by @code{find-file-noselect}
and by the default revert function (@pxref{Reverting}).
@cindex new file message
@cindex file open error
If reading the file got an error because the file does not exist, but
its directory does exist, the caller should pass a non-@code{nil} value
for @var{error}. In that case, @code{after-find-file} issues a warning:
@samp{(New File)}. For more serious errors, the caller should usually not
call @code{after-find-file}.
If @var{warn} is non-@code{nil}, then this function issues a warning
if an auto-save file exists and is more recent than the visited file.
The last thing @code{after-find-file} does is call all the functions
in @code{find-file-hooks}.
@end defun
@node Saving Buffers
@section Saving Buffers
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 @dfn{save} the buffer, which
means copying the contents of the buffer into the file.
@deffn Command save-buffer &optional backup-option
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.
@code{save-buffer} is responsible for making backup files. Normally,
@var{backup-option} is @code{nil}, and @code{save-buffer} makes a backup
file only if this is the first save or if the buffer was previously
modified. Other values for @var{backup-option} request the making of
backup files in other circumstances:
@itemize @bullet
With an argument of 4 or 64, reflecting 1 or 3 @kbd{C-u}'s, the
@code{save-buffer} function marks this version of the file to be
backed up when the buffer is next saved.
With an argument of 16 or 64, reflecting 2 or 3 @kbd{C-u}'s, the
@code{save-buffer} function unconditionally backs up the previous
version of the file before saving it.
@end itemize
@end deffn
@deffn Command save-some-buffers &optional save-silently-p exiting
This command saves some modified file-visiting buffers. Normally it
asks the user about each buffer. But if @var{save-silently-p} is
non-@code{nil}, it saves all the file-visiting buffers without querying
the user.
The optional @var{exiting} argument, if non-@code{nil}, requests this
function to offer also to save certain other buffers that are not
visiting files. These are buffers that have a non-@code{nil} local
value of @code{buffer-offer-save}. (A user who says yes to saving one
of these is asked to specify a file name to use.) The
@code{save-buffers-kill-emacs} function passes a non-@code{nil} value
for this argument.
@end deffn
@defvar buffer-offer-save
When this variable is non-@code{nil} in a buffer, Emacs offers to save
the buffer on exit even if the buffer is not visiting a file. The
variable is automatically local in all buffers. Normally, Mail mode
(used for editing outgoing mail) sets this to @code{t}.
@end defvar
@deffn Command write-file filename
This function writes the current buffer into file @var{filename}, makes
the buffer visit that file, and marks it not modified. Then it renames
the buffer based on @var{filename}, appending a string like @samp{<2>}
if necessary to make a unique buffer name. It does most of this work by
calling @code{set-visited-file-name} and @code{save-buffer}.
@end deffn
@defvar write-file-hooks
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}, the file is considered already written and the rest of
the functions are not called, nor is the usual code for writing the file
If a function in @code{write-file-hooks} returns non-@code{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))
@end example
You might wish to save the file modes value returned by
@code{backup-buffer} and use that to set the mode bits of the file that
you write. This is what @code{save-buffer} normally does.
Even though this is not a normal hook, you can use @code{add-hook} and
@code{remove-hook} to manipulate the list. @xref{Hooks}.
@end defvar
@c Emacs 19 feature
@defvar local-write-file-hooks
This works just like @code{write-file-hooks}, but it is intended
to be made local to particular buffers. It's not a good idea to make
@code{write-file-hooks} local to a buffer---use this variable instead.
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.
@end defvar
@c Emacs 19 feature
@defvar write-contents-hooks
This works just like @code{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. Typically major mode commands make
buffer-local bindings for this variable.
@end defvar
@c Emacs 19 feature
@defvar after-save-hook
This normal hook runs after a buffer has been saved in its visited file.
@end defvar
@defvar file-precious-flag
If this variable is non-@code{nil}, then @code{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.
(This feature worked differently in older Emacs versions.)
Some modes set this non-@code{nil} locally in particular buffers.
@end defvar
@defopt require-final-newline
This variable determines whether files may be written out that do
@emph{not} end with a newline. If the value of the variable is
@code{t}, then @code{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-@code{nil}, but not @code{t}, then
@code{save-buffer} asks the user whether to add a newline each time the
case arises.
If the value of the variable is @code{nil}, then @code{save-buffer}
doesn't add newlines at all. @code{nil} is the default value, but a few
major modes set it to @code{t} in particular buffers.
@end defopt
@node Reading from Files
@comment node-name, next, previous, up
@section Reading from Files
You can copy a file from the disk and insert it into a buffer
using the @code{insert-file-contents} function. Don't use the user-level
command @code{insert-file} in a Lisp program, as that sets the mark.
@defun insert-file-contents filename &optional visit beg end replace
This function inserts the contents of file @var{filename} into the
current buffer after point. It returns a list of the absolute file name
and the length of the data inserted. An error is signaled if
@var{filename} is not the name of a file that can be read.
To set up saved text properties, @code{insert-file-contents} calls the
functions in the list @code{after-insert-file-functions}. For more
information, see @ref{Saving Properties}.
If @var{visit} is non-@code{nil}, this function additionally marks the
buffer as unmodified and sets up various fields in the buffer so that it
is visiting the file @var{filename}: these include the buffer's visited
file name and its last save file modtime. This feature is used by
@code{find-file-noselect} and you probably should not use it yourself.
If @var{beg} and @var{end} are non-@code{nil}, they should be integers
specifying the portion of the file to insert. In this case, @var{visit}
must be @code{nil}. For example,
(insert-file-contents filename nil 0 500)
@end example
inserts the first 500 characters of a file.
If the argument @var{replace} is non-@code{nil}, it means to replace the
contents of the buffer (actually, just the accessible portion) with the
contents of the file. This is better than simply deleting the buffer
contents and inserting the whole file, because (1) it preserves some
marker positions and (2) it puts less data in the undo list.
@end defun
If you want to pass a file name to another process so that another
program can read the file, use the function @code{file-local-copy}; see
@ref{Magic File Names}.
@node Writing to Files
@comment node-name, next, previous, up
@section Writing to Files
You can write the contents of a buffer, or part of a buffer, directly
to a file on disk using the @code{append-to-file} and
@code{write-region} functions. Don't use these functions to write to
files that are being visited; that could cause confusion in the
mechanisms for visiting.
@deffn Command append-to-file start end filename
This function appends the contents of the region delimited by
@var{start} and @var{end} in the current buffer to the end of file
@var{filename}. If that file does not exist, it is created. This
function returns @code{nil}.
An error is signaled if @var{filename} specifies a nonwritable file,
or a nonexistent file in a directory where files cannot be created.
@end deffn
@deffn Command write-region start end filename &optional append visit
This function writes the region delimited by @var{start} and @var{end}
in the current buffer into the file specified by @var{filename}.
@c Emacs 19 feature
If @var{start} is a string, then @code{write-region} writes or appends
that string, rather than text from the buffer.
If @var{append} is non-@code{nil}, then the specified text is appended
to the existing file contents (if any).
If @var{visit} is @code{t}, then Emacs establishes an association
between the buffer and the file: the buffer is then visiting that file.
It also sets the last file modification time for the current buffer to
@var{filename}'s modtime, and marks the buffer as not modified. This
feature is used by @code{save-buffer}, but you probably should not use
it yourself.
@c Emacs 19 feature
If @var{visit} is a string, it specifies the file name to visit. This
way, you can write the data to one file (@var{filename}) while recording
the buffer as visiting another file (@var{visit}). The argument
@var{visit} is used in the echo area message and also for file locking;
@var{visit} is stored in @code{buffer-file-name}. This feature is used
to implement @code{file-precious-flag}; don't use it yourself unless you
really know what you're doing.
To output information about text properties, @code{write-region} calls
the functions in the list @code{write-region-annotation-functions}. For
more information, see @ref{Saving Properties}.
Normally, @code{write-region} displays a message @samp{Wrote file
@var{filename}} in the echo area. If @var{visit} is neither @code{t}
nor @code{nil} nor a string, then this message is inhibited. This
feature is useful for programs that use files for internal purposes,
files which the user does not need to know about.
@end deffn
@node File Locks
@section File Locks
@cindex file locks
When two users edit the same file at the same time, they are likely to
interfere with each other. Emacs tries to prevent this situation from
arising by recording a @dfn{file lock} when a file is being modified.
Emacs can then detect the first attempt to modify a buffer visiting a
file that is locked by another Emacs job, and ask the user what to do.
File locks do not work properly when multiple machines can share
file systems, such as with NFS. Perhaps a better file locking system
will be implemented in the future. When file locks do not work, it is
possible for two users to make changes simultaneously, but Emacs can
still warn the user who saves second. Also, the detection of
modification of a buffer visiting a file changed on disk catches some
cases of simultaneous editing; see @ref{Modification Time}.
@defun file-locked-p filename
This function returns @code{nil} if the file @var{filename} is not
locked by this Emacs process. It returns @code{t} if it is locked by
this Emacs, and it returns the name of the user who has locked it if it
is locked by someone else.
(file-locked-p "foo")
@result{} nil
@end group
@end example
@end defun
@defun lock-buffer &optional filename
This function locks the file @var{filename}, if the current buffer is
modified. The argument @var{filename} defaults to the current buffer's
visited file. Nothing is done if the current buffer is not visiting a
file, or is not modified.
@end defun
@defun unlock-buffer
This function unlocks the file being visited in the current buffer,
if the buffer is modified. If the buffer is not modified, then
the file should not be locked, so this function does nothing. It also
does nothing if the current buffer is not visiting a file.
@end defun
@defun ask-user-about-lock file other-user
This function is called when the user tries to modify @var{file}, but it
is locked by another user name @var{other-user}. The value it returns
determines what happens next:
@itemize @bullet
A value of @code{t} says to grab the lock on the file. Then
this user may edit the file and @var{other-user} loses the lock.
A value of @code{nil} says to ignore the lock and let this
user edit the file anyway.
@kindex file-locked
This function may instead signal a @code{file-locked} error, in which
case the change that the user was about to make does not take place.
The error message for this error looks like this:
@error{} File is locked: @var{file} @var{other-user}
@end example
where @code{file} is the name of the file and @var{other-user} is the
name of the user who has locked the file.
@end itemize
The default definition of this function asks the user to choose what
to do. If you wish, you can replace the @code{ask-user-about-lock}
function with your own version that decides in another way. The code
for its usual definition is in @file{userlock.el}.
@end defun
@node Information about Files
@section Information about Files
The functions described in this section are similar in as much as
they all operate on strings which are interpreted as file names. All
have names that begin with the word @samp{file}. These functions all
return information about actual files or directories, so their
arguments must all exist as actual files or directories unless
otherwise noted.
Most of the file-oriented functions take a single argument,
@var{filename}, which must be a string. The file name is expanded using
@code{expand-file-name}, so @file{~} is handled correctly, as are
relative file names (including @samp{../}). These functions don't
recognize environment variable substitutions such as @samp{$HOME}.
@xref{File Name Expansion}.
* Testing Accessibility:: Is a given file readable? Writable?
* Kinds of Files:: Is it a directory? A symbolic link?
* Truenames:: Eliminating symbolic links from a file name.
* File Attributes:: How large is it? Any other names? Etc.
@end menu
@node Testing Accessibility
@comment node-name, next, previous, up
@subsection Testing Accessibility
@cindex accessibility of a file
@cindex file accessibility
These functions test for permission to access a file in specific ways.
@defun file-exists-p filename
This function returns @code{t} if a file named @var{filename} appears
to exist. This does not mean you can necessarily read the file, only
that you can find out its attributes. (On Unix, this is true if the
file exists and you have execute permission on the containing
directories, regardless of the protection of the file itself.)
If the file does not exist, or if fascist access control policies
prevent you from finding the attributes of the file, this function
returns @code{nil}.
@end defun
@defun file-readable-p filename
This function returns @code{t} if a file named @var{filename} exists
and you can read it. It returns @code{nil} otherwise.
(file-readable-p "files.texi")
@result{} t
@end group
(file-exists-p "/usr/spool/mqueue")
@result{} t
@end group
(file-readable-p "/usr/spool/mqueue")
@result{} nil
@end group
@end example
@end defun
@c Emacs 19 feature
@defun file-executable-p filename
This function returns @code{t} if a file named @var{filename} exists and
you can execute it. It returns @code{nil} otherwise. If the file is a
directory, execute permission means you can check the existence and
attributes of files inside the directory, and open those files if their
modes permit.
@end defun
@defun file-writable-p filename
This function returns @code{t} if the file @var{filename} can be written or
created by you. It is writable if the file exists and you can write it.
It is creatable if the file does not exist, but the specified directory
does exist and you can write in that directory. @code{file-writable-p}
returns @code{nil} otherwise.
In the third example below, @file{foo} is not writable because the
parent directory does not exist, even though the user could create such
a directory.
(file-writable-p "~/foo")
@result{} t
@end group
(file-writable-p "/foo")
@result{} nil
@end group
(file-writable-p "~/no-such-dir/foo")
@result{} nil