files.texi 149 KB
Newer Older
Glenn Morris's avatar
Glenn Morris committed
1 2
@c -*-texinfo-*-
@c This is part of the GNU Emacs Lisp Reference Manual.
Paul Eggert's avatar
Paul Eggert committed
3
@c Copyright (C) 1990-1995, 1998-1999, 2001-2019 Free Software
4
@c Foundation, Inc.
Glenn Morris's avatar
Glenn Morris committed
5
@c See the file elisp.texi for copying conditions.
6
@node Files
Glenn Morris's avatar
Glenn Morris committed
7 8
@chapter Files

9
  This chapter describes the Emacs Lisp functions and variables to
Chong Yidong's avatar
Chong Yidong committed
10
find, create, view, save, and otherwise work with files and
11
directories.  A few other file-related functions are described in
Glenn Morris's avatar
Glenn Morris committed
12 13 14 15
@ref{Buffers}, and those related to backups and auto-saving are
described in @ref{Backups and Auto-Saving}.

  Many of the file functions take one or more arguments that are file
Chong Yidong's avatar
Chong Yidong committed
16 17
names.  A file name is a string.  Most of these functions expand file
name arguments using the function @code{expand-file-name}, so that
Glenn Morris's avatar
Glenn Morris committed
18
@file{~} is handled correctly, as are relative file names (including
19
@file{../} and the empty string).  @xref{File Name Expansion}.
20 21 22

  In addition, certain @dfn{magic} file names are handled specially.
For example, when a remote file name is specified, Emacs accesses the
Chong Yidong's avatar
Chong Yidong committed
23 24
file over the network via an appropriate protocol.  @xref{Remote
Files,, Remote Files, emacs, The GNU Emacs Manual}.  This handling is
25 26 27
done at a very low level, so you may assume that all the functions
described in this chapter accept magic file names as file name
arguments, except where noted.  @xref{Magic File Names}, for details.
Glenn Morris's avatar
Glenn Morris committed
28 29 30 31

  When file I/O functions signal Lisp errors, they usually use the
condition @code{file-error} (@pxref{Handling Errors}).  The error
message is in most cases obtained from the operating system, according
Leo Liu's avatar
Leo Liu committed
32
to locale @code{system-messages-locale}, and decoded using coding system
Glenn Morris's avatar
Glenn Morris committed
33 34 35 36 37 38 39 40 41 42
@code{locale-coding-system} (@pxref{Locales}).

@menu
* 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.
43
* Changing Files::           Renaming files, changing permissions, etc.
44
* Files and Storage::        Surviving power and media failures
Glenn Morris's avatar
Glenn Morris committed
45 46
* File Names::               Decomposing and expanding file names.
* Contents of Directories::  Getting a list of the files in a directory.
Glenn Morris's avatar
Glenn Morris committed
47
* Create/Delete Dirs::       Creating and Deleting Directories.
48
* Magic File Names::         Special handling for certain file names.
Glenn Morris's avatar
Glenn Morris committed
49 50 51 52 53 54 55 56 57 58
* Format Conversion::        Conversion to and from various file formats.
@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
Paul Eggert's avatar
Paul Eggert committed
59
file @dfn{the visited file} of the buffer.
Glenn Morris's avatar
Glenn Morris committed
60 61

  A file and a buffer are two different things.  A file is information
Chong Yidong's avatar
Chong Yidong committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
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).  When a buffer is visiting a file, it contains information
copied from the file.  The copy in the buffer is what you modify with
editing commands.  Changes to the buffer do not change the file; to
make the changes permanent, you must @dfn{save} the buffer, which
means copying the altered buffer contents back into the file.

  Despite 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
that 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.
Glenn Morris's avatar
Glenn Morris committed
77 78 79 80 81 82 83 84

@menu
* 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
85 86
@cindex visiting files, functions for
@cindex how to visit files
Glenn Morris's avatar
Glenn Morris committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

  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.

  In a Lisp program, if you want to look at the contents of a file but
not alter it, the fastest way is to use @code{insert-file-contents} in a
temporary buffer.  Visiting the file is not necessary and takes longer.
@xref{Reading from Files}.

@deffn Command find-file filename &optional wildcards
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.

Aside from some technical details, the body of the @code{find-file}
function is basically equivalent to:

@smallexample
(switch-to-buffer (find-file-noselect filename nil nil wildcards))
@end smallexample

@noindent
112
(See @code{switch-to-buffer} in @ref{Switching Buffers}.)
Glenn Morris's avatar
Glenn Morris committed
113 114 115 116 117 118 119 120 121

If @var{wildcards} is non-@code{nil}, which is always true in an
interactive call, then @code{find-file} expands wildcard characters in
@var{filename} and visits all the matching files.

When @code{find-file} is called interactively, it prompts for
@var{filename} in the minibuffer.
@end deffn

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
@deffn Command find-file-literally filename
This command visits @var{filename}, like @code{find-file} does, but it
does not perform any format conversions (@pxref{Format Conversion}),
character code conversions (@pxref{Coding Systems}), or end-of-line
conversions (@pxref{Coding System Basics, End of line conversion}).
The buffer visiting the file is made unibyte, and its major mode is
Fundamental mode, regardless of the file name.  File local variable
specifications  in the file (@pxref{File Local Variables}) are
ignored, and automatic decompression and adding a newline at the end
of the file due to @code{require-final-newline} (@pxref{Saving
Buffers, require-final-newline}) are also disabled.

Note that if Emacs already has a buffer visiting the same file
non-literally, it will not visit the same file literally, but instead
just switch to the existing buffer.  If you want to be sure of
accessing a file's contents literally, you should create a temporary
buffer and then read the file contents into it using
@code{insert-file-contents-literally} (@pxref{Reading from Files}).
@end deffn

Glenn Morris's avatar
Glenn Morris committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
@defun find-file-noselect filename &optional nowarn rawfile wildcards
This function is the guts of all the file-visiting functions.  It
returns a buffer visiting the file @var{filename}.  You may make the
buffer current or display it in a window if you wish, but this
function does not do so.

The function returns an existing buffer if there is one; otherwise it
creates a new buffer and reads the file into it.  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, this function asks the user
whether to reread the changed file.  If the user says @samp{yes}, any
edits previously made in the buffer are lost.

Reading the file involves decoding the file's contents (@pxref{Coding
Systems}), including end-of-line conversion, and format conversion
(@pxref{Format Conversion}).  If @var{wildcards} is non-@code{nil},
then @code{find-file-noselect} expands wildcard characters in
@var{filename} and visits all the matching files.

This function displays warning or advisory messages in various peculiar
cases, unless the optional argument @var{nowarn} is non-@code{nil}.  For
example, if it 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 normally 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-hook}.

If the optional argument @var{rawfile} is non-@code{nil}, then
@code{after-find-file} is not called, and the
@code{find-file-not-found-functions} are not run in case of failure.
What's more, a non-@code{nil} @var{rawfile} value suppresses coding
system conversion and format conversion.

The @code{find-file-noselect} function usually returns the buffer that
is visiting the file @var{filename}.  But, if wildcards are actually
used and expanded, it returns a list of buffers that are visiting the
various files.

@example
@group
(find-file-noselect "/etc/fstab")
     @result{} #<buffer fstab>
@end group
@end example
@end defun

@deffn Command find-file-other-window filename &optional wildcards
This command selects a buffer visiting the file @var{filename}, but
196 197 198
does so in a window other than the selected window.  It may use
another existing window or split a window; see @ref{Switching
Buffers}.
Glenn Morris's avatar
Glenn Morris committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221

When this command is called interactively, it prompts for
@var{filename}.
@end deffn

@deffn Command find-file-read-only filename &optional wildcards
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
@var{filename}.
@end deffn

@defopt find-file-wildcards
If this variable is non-@code{nil}, then the various @code{find-file}
commands check for wildcard characters and visit all the files that
match them (when invoked interactively or when their @var{wildcards}
argument is non-@code{nil}).  If this option is @code{nil}, then
the @code{find-file} commands ignore their @var{wildcards} argument
and never treat wildcard characters specially.
@end defopt

222
@defopt find-file-hook
Glenn Morris's avatar
Glenn Morris committed
223 224 225 226 227 228
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 is a normal hook.  @xref{Hooks}.
229
@end defopt
Glenn Morris's avatar
Glenn Morris committed
230 231 232 233 234 235 236 237 238 239 240 241 242

@defvar find-file-not-found-functions
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 in many cases only some of the functions are called.
@end defvar

243 244 245
@defvar find-file-literally
This buffer-local variable, if set to a non-@code{nil} value, makes
@code{save-buffer} behave as if the buffer were visiting its file
246
literally, i.e., without conversions of any kind.  The command
247
@code{find-file-literally} sets this variable's local value, but other
248
equivalent functions and commands can do that as well, e.g., to avoid
249
automatic addition of a newline at the end of the file.  This variable
250
is permanent local, so it is unaffected by changes of major modes.
251 252
@end defvar

Glenn Morris's avatar
Glenn Morris committed
253 254 255 256 257 258 259
@node Subroutines of Visiting
@subsection Subroutines of Visiting

  The @code{find-file-noselect} function uses two important subroutines
which are sometimes useful in user Lisp code: @code{create-file-buffer}
and @code{after-find-file}.  This section explains how to use them.

Glenn Morris's avatar
Glenn Morris committed
260 261 262
@c FIXME This does not describe the default behavior, because
@c uniquify is enabled by default and advises this function.
@c This is confusing.  uniquify should be folded into the function proper.
Glenn Morris's avatar
Glenn Morris committed
263 264 265 266 267
@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}.
Glenn Morris's avatar
Glenn Morris committed
268 269
Note that the @file{uniquify} library affects the result of this
function.  @xref{Uniquify,,, emacs, The GNU Emacs Manual}.
Glenn Morris's avatar
Glenn Morris committed
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334

@strong{Please note:} @code{create-file-buffer} does @emph{not}
associate the new buffer with a file and does not select the buffer.
It also does not use the default major mode.

@example
@group
(create-file-buffer "foo")
     @result{} #<buffer foo>
@end group
@group
(create-file-buffer "foo")
     @result{} #<buffer foo<2>>
@end group
@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 noauto after-find-file-from-revert-buffer nomodes
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.

If @var{noauto} is non-@code{nil}, that says not to enable or disable
Auto-Save mode.  The mode remains enabled if it was enabled before.

If @var{after-find-file-from-revert-buffer} is non-@code{nil}, that
means this call was from @code{revert-buffer}.  This has no direct
effect, but some mode functions and hook functions check the value
of this variable.

If @var{nomodes} is non-@code{nil}, that means don't alter the buffer's
major mode, don't process local variables specifications in the file,
and don't run @code{find-file-hook}.  This feature is used by
@code{revert-buffer} in some cases.

The last thing @code{after-find-file} does is call all the functions
in the list @code{find-file-hook}.
@end defun

@node Saving Buffers
@section Saving Buffers
@cindex 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
335 336 337
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} hook.
Glenn Morris's avatar
Glenn Morris committed
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369

@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 since visiting the file.  Other
values for @var{backup-option} request the making of backup files in
other circumstances:

@itemize @bullet
@item
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.

@item
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.

@item
With an argument of 0, unconditionally do @emph{not} make any backup file.
@end itemize
@end deffn

@deffn Command save-some-buffers &optional save-silently-p pred
@anchor{Definition of save-some-buffers}
This command saves some modified file-visiting buffers.  Normally it
asks the user about each buffer.  But if @var{save-silently-p} is
370
non-@code{nil}, it saves all the file-visiting buffers without
371
querying the user.
Glenn Morris's avatar
Glenn Morris committed
372

373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
@vindex save-some-buffers-default-predicate
The optional @var{pred} argument provides a predicate that controls
which buffers to ask about (or to save silently if
@var{save-silently-p} is non-@code{nil}).  If @var{pred} is
@code{nil}, that means to use the value of
@code{save-some-buffers-default-predicate} instead of @var{pred}.  If
the result is @code{nil}, it means ask only about file-visiting
buffers.  If it is @code{t}, that means also offer to save certain
other non-file buffers---those that have a non-@code{nil} buffer-local
value of @code{buffer-offer-save} (@pxref{Killing Buffers}).  A user
who says @samp{yes} to saving a non-file buffer is asked to specify
the file name to use.  The @code{save-buffers-kill-emacs} function
passes the value @code{t} for @var{pred}.

If the predicate is neither @code{t} nor @code{nil}, then it should be
Glenn Morris's avatar
Glenn Morris committed
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
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}
value in a certain buffer, that means do offer to save that buffer.
@end deffn

@deffn Command write-file filename &optional confirm
@anchor{Definition of write-file}
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} (@pxref{Buffer File Name}) and
@code{save-buffer}.

If @var{confirm} is non-@code{nil}, that means to ask for confirmation
before overwriting an existing file.  Interactively, confirmation is
required, unless the user supplies a prefix argument.

406
If @var{filename} is a directory name (@pxref{Directory Names}),
Glenn Morris's avatar
Glenn Morris committed
407 408 409 410 411 412
@code{write-file} uses the name of the visited file, in directory
@var{filename}.  If the buffer is not visiting a file, it uses the
buffer name instead.
@end deffn

  Saving a buffer runs several hooks.  It also performs format
413 414 415 416 417
conversion (@pxref{Format Conversion}).  Note that these hooks,
described below, are only run by @code{save-buffer}, they are not run
by other primitives and functions that write buffer text to files, and
in particular auto-saving (@pxref{Auto-Saving}) doesn't run these
hooks.
Glenn Morris's avatar
Glenn Morris committed
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436

@defvar write-file-functions
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
executed.

If a function in @code{write-file-functions} returns non-@code{nil}, it
is responsible for making a backup file (if that is appropriate).
To do so, execute the following code:

@example
(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 (if non-@code{nil}) to set the mode
bits of the file that you write.  This is what @code{save-buffer}
437
normally does.  @xref{Making Backups,, Making Backup Files}.
Glenn Morris's avatar
Glenn Morris committed
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461

The hook functions in @code{write-file-functions} are also responsible
for encoding the data (if desired): they must choose a suitable coding
system and end-of-line conversion (@pxref{Lisp and Coding Systems}),
perform the encoding (@pxref{Explicit Encoding}), and set
@code{last-coding-system-used} to the coding system that was used
(@pxref{Encoding and I/O}).

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} will reset it.
If this is not what you want, you might like to use
@code{write-contents-functions} instead.

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 write-contents-functions
This works just like @code{write-file-functions}, but it is intended
for hooks that pertain to the buffer's contents, not to the particular
462 463 464 465 466 467
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} does not.
Glenn Morris's avatar
Glenn Morris committed
468 469 470 471

If any of the functions in this hook returns non-@code{nil}, the file
is considered already written and the rest are not called and neither
are the functions in @code{write-file-functions}.
472 473 474 475 476 477 478

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} value,
@code{save-buffer} 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.
Glenn Morris's avatar
Glenn Morris committed
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
@end defvar

@defopt before-save-hook
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 @file{copyright.el} program uses
this hook to make sure the file you are saving has the current year in
its copyright notice.
@end defopt

@c Emacs 19 feature
@defopt after-save-hook
This normal hook runs after a buffer has been saved in its visited file.
One use of this hook is in Fast Lock mode; it uses this hook to save the
highlighting information in a cache file.
@end defopt

@defopt 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.

As a side effect, backups are necessarily made by copying.  @xref{Rename
or Copy}.  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-@code{nil} buffer-local value
in particular buffers.
@end defopt

@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
515 516 517 518 519 520 521
@code{t}, then @code{save-buffer} silently adds a newline at the end
of the buffer whenever it does not already end in one.  If the value
is @code{visit}, Emacs adds a missing newline just after it visits the
file.  If the value is @code{visit-save}, Emacs adds a missing newline
both on visiting and on saving.  For any other non-@code{nil} value,
@code{save-buffer} asks the user whether to add a newline each time
the case arises.
Glenn Morris's avatar
Glenn Morris committed
522 523 524 525 526 527 528 529 530 531 532 533 534

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

  See also the function @code{set-visited-file-name} (@pxref{Buffer File
Name}).

@node Reading from Files
@section Reading from Files
@cindex reading from files

Chong Yidong's avatar
Chong Yidong committed
535 536 537
  To copy the contents of a file into a buffer, use the function
@code{insert-file-contents}.  (Don't use the command
@code{insert-file} in a Lisp program, as that sets the mark.)
Glenn Morris's avatar
Glenn Morris committed
538 539 540 541 542 543 544

@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.

545 546 547 548
This function checks the file contents against the defined file
formats, and converts the file contents if appropriate and also calls
the functions in the list @code{after-insert-file-functions}.
@xref{Format Conversion}.  Normally, one of the functions in the
Glenn Morris's avatar
Glenn Morris committed
549 550
@code{after-insert-file-functions} list determines the coding system
(@pxref{Coding Systems}) used for decoding the file's contents,
551
including end-of-line conversion.  However, if the file contains null
552
bytes, it is by default visited without any code conversions.
553
@xref{Lisp and Coding Systems, inhibit-nul-byte-detection}.
Glenn Morris's avatar
Glenn Morris committed
554 555 556 557 558 559 560

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.

561 562 563
If @var{beg} and @var{end} are non-@code{nil}, they should be numbers
that are byte offsets specifying the portion of the file to insert.
In this case, @var{visit} must be @code{nil}.  For example,
Glenn Morris's avatar
Glenn Morris committed
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583

@example
(insert-file-contents filename nil 0 500)
@end example

@noindent
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.

It is possible to read a special file (such as a FIFO or an I/O device)
with @code{insert-file-contents}, as long as @var{replace} and
@var{visit} are @code{nil}.
@end defun

@defun insert-file-contents-literally filename &optional visit beg end replace
584
This function works like @code{insert-file-contents} except that it
585 586 587
does not run @code{after-insert-file-functions}, and does not do
format decoding, character code conversion, automatic uncompression,
and so on.
Glenn Morris's avatar
Glenn Morris committed
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
@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
@section Writing to Files
@cindex 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.

When called from Lisp, this function is completely equivalent to:

@example
(write-region start end filename t)
@end example
@end deffn

@deffn Command write-region start end filename &optional append visit lockname mustbenew
This function writes the region delimited by @var{start} and @var{end}
in the current buffer into the file specified by @var{filename}.

If @var{start} is @code{nil}, then the command writes the entire buffer
contents (@emph{not} just the accessible portion) to the file and
ignores @var{end}.

@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.  @var{end} is ignored in
this case.

If @var{append} is non-@code{nil}, then the specified text is appended
634 635
to the existing file contents (if any).  If @var{append} is a
number, @code{write-region} seeks to that byte offset from the start
Glenn Morris's avatar
Glenn Morris committed
636 637 638 639 640 641
of the file and writes the data from there.

If @var{mustbenew} is non-@code{nil}, then @code{write-region} asks
for confirmation if @var{filename} names an existing file.  If
@var{mustbenew} is the symbol @code{excl}, then @code{write-region}
does not ask for confirmation, but instead it signals an error
642 643 644 645
@code{file-already-exists} if the file already exists.  Although
@code{write-region} normally follows a symbolic link and creates the
pointed-to file if the symbolic link is dangling, it does not follow
symbolic links if @var{mustbenew} is @code{excl}.
Glenn Morris's avatar
Glenn Morris committed
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678

The test for an existing file, when @var{mustbenew} is @code{excl}, uses
a special system feature.  At least for files on a local disk, there is
no chance that some other program could create a file of the same name
before Emacs does, without Emacs's noticing.

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.

The optional argument @var{lockname}, if non-@code{nil}, specifies the
file name to use for purposes of locking and unlocking, overriding
@var{filename} and @var{visit} for that purpose.

The function @code{write-region} converts the data which it writes to
the appropriate file formats specified by @code{buffer-file-format}
and also calls the functions in the list
@code{write-region-annotate-functions}.
@xref{Format Conversion}.

Normally, @code{write-region} displays the message @samp{Wrote
679 680
@var{filename}} in the echo area.  This message is inhibited if
@var{visit} is neither @code{t} nor @code{nil} nor a string, or if
681
Emacs is operating in batch mode (@pxref{Batch Mode}).  This
Glenn Morris's avatar
Glenn Morris committed
682 683 684 685
feature is useful for programs that use files for internal purposes,
files that the user does not need to know about.
@end deffn

686 687 688 689 690 691 692 693 694
@defvar write-region-inhibit-fsync
If this variable's value is @code{nil}, @code{write-region} uses the
@code{fsync} system call after writing a file.  Although this slows
Emacs down, it lessens the risk of data loss after power failure.  If
the value is @code{t}, Emacs does not use @code{fsync}.  The default
value is @code{nil} when Emacs is interactive, and @code{t} when Emacs
runs in batch mode.  @xref{Files and Storage}.
@end defvar

Glenn Morris's avatar
Glenn Morris committed
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
@defmac with-temp-file file body@dots{}
@anchor{Definition of with-temp-file}
The @code{with-temp-file} macro evaluates the @var{body} forms with a
temporary buffer as the current buffer; then, at the end, it writes the
buffer contents into file @var{file}.  It kills the temporary buffer
when finished, restoring the buffer that was current before the
@code{with-temp-file} form.  Then it returns the value of the last form
in @var{body}.

The current buffer is restored even in case of an abnormal exit via
@code{throw} or error (@pxref{Nonlocal Exits}).

See also @code{with-temp-buffer} in @ref{Definition of
with-temp-buffer,, The Current Buffer}.
@end defmac

@node File Locks
@section File Locks
@cindex file locks
@cindex lock file
715
@cindex .#, lock file names
Glenn Morris's avatar
Glenn Morris committed
716 717 718 719

  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
720
modified.
Glenn Morris's avatar
Glenn Morris committed
721 722 723
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.
The file lock is really a file, a symbolic link with a special name,
724 725 726 727 728 729 730 731 732 733 734
stored in the same directory as the file you are editing.  The name is
constructed by prepending @file{.#} to the filename of the buffer.
The target of the symbolic link will be of the form
@code{@var{user}@@@var{host}.@var{pid}:@var{boot}}, where @var{user}
is replaced with the current username (from @code{user-login-name}),
@var{host} with the name of the host where Emacs is running (from
@code{system-name}), @var{pid} with Emacs's process id, and @var{boot}
with the time since the last reboot.  @code{:@var{boot}} is omitted if
the boot time is unavailable.  (On file systems that do not support
symbolic links, a regular file is used instead, with contents of the
form @code{@var{user}@@@var{host}.@var{pid}:@var{boot}}.)
Glenn Morris's avatar
Glenn Morris committed
735 736

  When you access files using NFS, there may be a small probability that
Paul Eggert's avatar
Paul Eggert committed
737
you and another user will both lock the same file simultaneously.
Glenn Morris's avatar
Glenn Morris committed
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
If this happens, it is possible for the two users to make changes
simultaneously, but Emacs will 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.  It returns @code{t} if it is locked by this Emacs process, and
it returns the name of the user who has locked it if it is locked by
some other job.

@example
@group
(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
762 763
file, or is not modified, or if the option @code{create-lockfiles} is
@code{nil}.
Glenn Morris's avatar
Glenn Morris committed
764 765 766 767 768 769
@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
770
does nothing if the current buffer is not visiting a file, or is not locked.
Glenn Morris's avatar
Glenn Morris committed
771 772
@end defun

773 774 775
@defopt create-lockfiles
If this variable is @code{nil}, Emacs does not lock files.
@end defopt
Glenn Morris's avatar
Glenn Morris committed
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808

@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 named @var{other-user}.  The default
definition of this function asks the user to say what to do.  The value
this function returns determines what Emacs does next:

@itemize @bullet
@item
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.

@item
A value of @code{nil} says to ignore the lock and let this
user edit the file anyway.

@item
@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:

@example
@error{} File is locked: @var{file} @var{other-user}
@end example

@noindent
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

If you wish, you can replace the @code{ask-user-about-lock} function
809
with your own version that makes the decision in another way.
Glenn Morris's avatar
Glenn Morris committed
810 811 812 813 814 815
@end defun

@node Information about Files
@section Information about Files
@cindex file, information about

Chong Yidong's avatar
Chong Yidong committed
816 817 818 819 820
  This section describes the functions for retrieving various types of
information about files (or directories or symbolic links), such as
whether a file is readable or writable, and its size.  These functions
all take arguments which are file names.  Except where noted, these
arguments need to specify existing files, or an error is signaled.
Glenn Morris's avatar
Glenn Morris committed
821

822 823
@cindex file names, trailing whitespace
@cindex trailing blanks in file names
Chong Yidong's avatar
Chong Yidong committed
824 825 826
  Be careful with file names that end in spaces.  On some filesystems
(notably, MS-Windows), trailing whitespace characters in file names
are silently and automatically ignored.
827

Glenn Morris's avatar
Glenn Morris committed
828 829 830
@menu
* Testing Accessibility::   Is a given file readable?  Writable?
* Kinds of Files::          Is it a directory?  A symbolic link?
Glenn Morris's avatar
Glenn Morris committed
831
* Truenames::               Eliminating symbolic links from a file name.
Chong Yidong's avatar
Chong Yidong committed
832 833
* File Attributes::         File sizes, modification times, etc.
* Extended Attributes::     Extended file attributes for access control.
Glenn Morris's avatar
Glenn Morris committed
834 835 836 837 838 839 840 841
* Locating Files::          How to find a file in standard places.
@end menu

@node Testing Accessibility
@subsection Testing Accessibility
@cindex accessibility of a file
@cindex file accessibility

Chong Yidong's avatar
Chong Yidong committed
842 843
  These functions test for permission to access a file for reading,
writing, or execution.  Unless explicitly stated otherwise, they
844
follow symbolic links.  @xref{Kinds of Files}.
Chong Yidong's avatar
Chong Yidong committed
845 846 847 848 849

  On some operating systems, more complex sets of access permissions
can be specified, via mechanisms such as Access Control Lists (ACLs).
@xref{Extended Attributes}, for how to query and set those
permissions.
Glenn Morris's avatar
Glenn Morris committed
850 851 852 853

@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
854 855 856 857
that you can find out its attributes.  (On GNU and other POSIX-like
systems, this is true if the file exists and you have execute
permission on the containing directories, regardless of the
permissions of the file itself.)
Glenn Morris's avatar
Glenn Morris committed
858

Chong Yidong's avatar
Chong Yidong committed
859 860
If the file does not exist, or if access control policies prevent you
from finding its attributes, this function returns @code{nil}.
Glenn Morris's avatar
Glenn Morris committed
861

862 863
Directories are files, so @code{file-exists-p} can return @code{t} when
given a directory.  However, because @code{file-exists-p} follows
864
symbolic links, it returns @code{t} for a symbolic link
Glenn Morris's avatar
Glenn Morris committed
865 866 867 868 869 870 871 872 873
name only if the target file exists.
@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.
@end defun

@defun file-executable-p filename
874 875 876 877 878
This function returns @code{t} if a file named @var{filename} exists
and you can execute it.  It returns @code{nil} otherwise.  On GNU and
other POSIX-like systems, 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.
Glenn Morris's avatar
Glenn Morris committed
879 880 881 882 883 884 885 886 887
@end defun

@defun file-writable-p filename
This function returns @code{t} if the file @var{filename} can be written
or created by you, and @code{nil} otherwise.  A file is writable if the
file exists and you can write it.  It is creatable if it does not exist,
but the specified directory does exist and you can write in that
directory.

Chong Yidong's avatar
Chong Yidong committed
888 889 890
In the example below, @file{foo} is not writable because the parent
directory does not exist, even though the user could create such a
directory.
Glenn Morris's avatar
Glenn Morris committed
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907

@example
@group
(file-writable-p "~/no-such-dir/foo")
     @result{} nil
@end group
@end example
@end defun

@defun file-accessible-directory-p dirname
This function returns @code{t} if you have permission to open existing
files in the directory whose name as a file is @var{dirname};
otherwise (or if there is no such directory), it returns @code{nil}.
The value of @var{dirname} may be either a directory name (such as
@file{/foo/}) or the file name of a file which is a directory
(such as @file{/foo}, without the final slash).

Chong Yidong's avatar
Chong Yidong committed
908 909
For example, from the following we deduce that any attempt to read a
file in @file{/foo/} will give an error:
Glenn Morris's avatar
Glenn Morris committed
910 911 912 913 914 915 916 917 918 919 920 921 922

@example
(file-accessible-directory-p "/foo")
     @result{} nil
@end example
@end defun

@defun access-file filename string
This function opens file @var{filename} for reading, then closes it and
returns @code{nil}.  However, if the open fails, it signals an error
using @var{string} as the error message text.
@end defun

923
@defun file-ownership-preserved-p filename &optional group
Glenn Morris's avatar
Glenn Morris committed
924 925 926 927
This function returns @code{t} if deleting the file @var{filename} and
then creating it anew would keep the file's owner unchanged.  It also
returns @code{t} for nonexistent files.

928 929 930
If the optional argument @var{group} is non-@code{nil}, this function
also checks that the file's group would be unchanged.

931
This function does not follow symbolic links.
Glenn Morris's avatar
Glenn Morris committed
932 933
@end defun

Chong Yidong's avatar
Chong Yidong committed
934 935 936 937 938 939 940
@defun file-modes filename
@cindex mode bits
@cindex file permissions
@cindex permissions, file
@cindex file modes
This function returns the @dfn{mode bits} of @var{filename}---an
integer summarizing its read, write, and execution permissions.
941 942
This function follows symbolic links.  If the file does not exist, the
return value is @code{nil}.
Glenn Morris's avatar
Glenn Morris committed
943

Chong Yidong's avatar
Chong Yidong committed
944 945 946 947 948 949 950 951 952 953
@xref{File permissions,,, coreutils, The @sc{gnu} @code{Coreutils}
Manual}, for a description of mode bits.  For example, if the
low-order bit is 1, the file is executable by all users; if the
second-lowest-order bit is 1, the file is writable by all users; etc.
The highest possible value is 4095 (7777 octal), meaning that everyone
has read, write, and execute permission, the @acronym{SUID} bit is set
for both others and group, and the sticky bit is set.

@xref{Changing Files}, for the @code{set-file-modes} function, which
can be used to set these permissions.
Glenn Morris's avatar
Glenn Morris committed
954 955 956

@example
@group
Chong Yidong's avatar
Chong Yidong committed
957 958
(file-modes "~/junk/diffs")
     @result{} 492               ; @r{Decimal integer.}
Glenn Morris's avatar
Glenn Morris committed
959 960
@end group
@group
Chong Yidong's avatar
Chong Yidong committed
961 962
(format "%o" 492)
     @result{} "754"             ; @r{Convert to octal.}
Glenn Morris's avatar
Glenn Morris committed
963
@end group
Chong Yidong's avatar
Chong Yidong committed
964

Glenn Morris's avatar
Glenn Morris committed
965
@group
Chong Yidong's avatar
Chong Yidong committed
966 967
(set-file-modes "~/junk/diffs" #o666)
     @result{} nil
Glenn Morris's avatar
Glenn Morris committed
968
@end group
Chong Yidong's avatar
Chong Yidong committed
969

Glenn Morris's avatar
Glenn Morris committed
970
@group
Chong Yidong's avatar
Chong Yidong committed
971 972
$ ls -l diffs
-rw-rw-rw- 1 lewis lewis 3063 Oct 30 16:00 diffs
Glenn Morris's avatar
Glenn Morris committed
973 974 975
@end group
@end example

Chong Yidong's avatar
Chong Yidong committed
976 977 978
@cindex MS-DOS and file modes
@cindex file modes and MS-DOS
@strong{MS-DOS note:} On MS-DOS, there is no such thing as an
Paul Eggert's avatar
Paul Eggert committed
979
executable file mode bit.  So @code{file-modes} considers a file
Chong Yidong's avatar
Chong Yidong committed
980 981
executable if its name ends in one of the standard executable
extensions, such as @file{.com}, @file{.bat}, @file{.exe}, and some
982
others.  Files that begin with the POSIX-standard @samp{#!} signature,
Chong Yidong's avatar
Chong Yidong committed
983 984
such as shell and Perl scripts, are also considered executable.
Directories are also reported as executable, for compatibility with
985
POSIX@.  These conventions are also followed by @code{file-attributes}
Chong Yidong's avatar
Chong Yidong committed
986
(@pxref{File Attributes}).
Glenn Morris's avatar
Glenn Morris committed
987 988 989 990
@end defun

@node Kinds of Files
@subsection Distinguishing Kinds of Files
991 992
@cindex file classification
@cindex classification of file types
993
@cindex symbolic links
Glenn Morris's avatar
Glenn Morris committed
994 995 996 997

  This section describes how to distinguish various kinds of files, such
as directories, symbolic links, and ordinary files.

998 999 1000 1001 1002 1003 1004 1005
  Symbolic links are ordinarily followed wherever they appear.  For
example, to interpret the file name @file{a/b/c}, any of @file{a},
@file{a/b}, and @file{a/b/c} can be symbolic links that are followed,
possibly recursively if the link targets are themselves symbolic
links.  However, a few functions do not follow symbolic links at the
end of a file name (@file{a/b/c} in this example).  Such a function
is said to @dfn{not follow symbolic links}.

Glenn Morris's avatar
Glenn Morris committed
1006
@defun file-symlink-p filename
1007 1008 1009
@cindex symbolic links
If the file @var{filename} is a symbolic link, this function does not
follow it and instead returns its link target
1010 1011
as a string.  (The link target string is not necessarily the full
absolute file name of the target; determining the full file name that
1012
the link points to is nontrivial, see below.)
1013 1014

If the file @var{filename} is not a symbolic link, or does not exist,
Glenn Morris's avatar
Glenn Morris committed
1015 1016
@code{file-symlink-p} returns @code{nil}.

1017 1018
Here are a few examples of using this function:

Glenn Morris's avatar
Glenn Morris committed
1019 1020
@example
@group
1021
(file-symlink-p "not-a-symlink")
Glenn Morris's avatar
Glenn Morris committed
1022 1023 1024 1025
     @result{} nil
@end group
@group
(file-symlink-p "sym-link")
1026
     @result{} "not-a-symlink"
Glenn Morris's avatar
Glenn Morris committed
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
@end group
@group
(file-symlink-p "sym-link2")
     @result{} "sym-link"
@end group
@group
(file-symlink-p "/bin")
     @result{} "/pub/bin"
@end group
@end example
1037 1038 1039

Note that in the third example, the function returned @file{sym-link},
but did not proceed to resolve it, although that file is itself a
1040 1041 1042
symbolic link.  That is because this function does not follow symbolic
links---the process of following the symbolic links does not apply to
the last component of the file name.
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070

The string that this function returns is what is recorded in the
symbolic link; it may or may not include any leading directories.
This function does @emph{not} expand the link target to produce a
fully-qualified file name, and in particular does not use the leading
directories, if any, of the @var{filename} argument if the link target
is not an absolute file name.  Here's an example:

@example
@group
(file-symlink-p "/foo/bar/baz")
     @result{} "some-file"
@end group
@end example

@noindent
Here, although @file{/foo/bar/baz} was given as a fully-qualified file
name, the result is not, and in fact does not have any leading
directories at all.  And since @file{some-file} might itself be a
symbolic link, you cannot simply prepend leading directories to it,
nor even naively use @code{expand-file-name} (@pxref{File Name
Expansion}) to produce its absolute file name.

For this reason, this function is seldom useful if you need to
determine more than just the fact that a file is or isn't a symbolic
link.  If you actually need the file name of the link target, use
@code{file-chase-links} or @code{file-truename}, described in
@ref{Truenames}.
Glenn Morris's avatar
Glenn Morris committed
1071 1072 1073 1074 1075
@end defun

@defun file-directory-p filename
This function returns @code{t} if @var{filename} is the name of an
existing directory, @code{nil} otherwise.
1076
This function follows symbolic links.
Glenn Morris's avatar
Glenn Morris committed
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106

@example
@group
(file-directory-p "~rms")
     @result{} t
@end group
@group
(file-directory-p "~rms/lewis/files.texi")
     @result{} nil
@end group
@group
(file-directory-p "~rms/lewis/no-such-file")
     @result{} nil
@end group
@group
(file-directory-p "$HOME")
     @result{} nil
@end group
@group
(file-directory-p
 (substitute-in-file-name "$HOME"))
     @result{} t
@end group
@end example
@end defun

@defun file-regular-p filename
This function returns @code{t} if the file @var{filename} exists and is
a regular file (not a directory, named pipe, terminal, or
other I/O device).
1107
This function follows symbolic links.
Glenn Morris's avatar
Glenn Morris committed
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
@end defun

@node Truenames
@subsection Truenames
@cindex truename (of file)

  The @dfn{truename} of a file is the name that you get by following
symbolic links at all levels until none remain, then simplifying away
@samp{.}@: and @samp{..}@: appearing as name components.  This results
in a sort of canonical name for the file.  A file does not always have a
unique truename; the number of distinct truenames a file has is equal to
the number of hard links to the file.  However, truenames are useful
because they eliminate symbolic links as a cause of name variation.

@defun file-truename filename
1123 1124 1125
This function returns the truename of the file @var{filename}.  If the
argument is not an absolute file name, this function first expands it
against @code{default-directory}.
Glenn Morris's avatar
Glenn Morris committed
1126 1127 1128 1129 1130 1131

This function does not expand environment variables.  Only
@code{substitute-in-file-name} does that.  @xref{Definition of
substitute-in-file-name}.

If you may need to follow symbolic links preceding @samp{..}@:
Chong Yidong's avatar
Chong Yidong committed
1132 1133 1134
appearing as a name component, call @code{file-truename} without prior
direct or indirect calls to @code{expand-file-name}.  Otherwise, the
file name component immediately preceding @samp{..} will be
Paul Eggert's avatar
Paul Eggert committed
1135
simplified away before @code{file-truename} is called.  To
Chong Yidong's avatar
Chong Yidong committed
1136 1137
eliminate the need for a call to @code{expand-file-name},
@code{file-truename} handles @samp{~} in the same way that
1138 1139 1140 1141 1142
@code{expand-file-name} does.

If the target of a symbolic links has remote file name syntax,
@code{file-truename} returns it quoted.  @xref{File Name Expansion,,
Functions that Expand Filenames}.
Glenn Morris's avatar
Glenn Morris committed
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
@end defun

@defun file-chase-links filename &optional limit
This function follows symbolic links, starting with @var{filename},
until it finds a file name which is not the name of a symbolic link.
Then it returns that file name.  This function does @emph{not} follow
symbolic links at the level of parent directories.

If you specify a number for @var{limit}, then after chasing through
that many links, the function just returns what it has even if that is
still a symbolic link.
@end defun

  To illustrate the difference between @code{file-chase-links} and
@code{file-truename}, suppose that @file{/usr/foo} is a symbolic link to
the directory @file{/home/foo}, and @file{/home/foo/hello} is an
ordinary file (or at least, not a symbolic link) or nonexistent.  Then
we would have:

@example
(file-chase-links "/usr/foo/hello")
     ;; @r{This does not follow the links in the parent directories.}
     @result{} "/usr/foo/hello"
(file-truename "/usr/foo/hello")
     ;; @r{Assuming that @file{/home} is not a symbolic link.}
     @result{} "/home/foo/hello"
@end example

Chong Yidong's avatar
Chong Yidong committed
1171 1172 1173 1174 1175 1176 1177 1178
@defun file-equal-p file1 file2
This function returns @code{t} if the files @var{file1} and
@var{file2} name the same file.  This is similar to comparing their
truenames, except that remote file names are also handled in an
appropriate manner.  If @var{file1} or @var{file2} does not exist, the
return value is unspecified.
@end defun

1179 1180 1181 1182
@defun file-name-case-insensitive-p filename
Sometimes file names or their parts need to be compared as strings, in
which case it's important to know whether the underlying filesystem is
case-insensitive.  This function returns @code{t} if file
1183
@var{filename} is on a case-insensitive filesystem.  It always returns
Paul Eggert's avatar
Paul Eggert committed
1184
@code{t} on MS-DOS and MS-Windows.  On Cygwin and macOS,
1185 1186 1187
filesystems may or may not be case-insensitive, and the function tries
to determine case-sensitivity by a runtime test.  If the test is
inconclusive, the function returns @code{t} on Cygwin and @code{nil}
Paul Eggert's avatar
Paul Eggert committed
1188
on macOS.
1189 1190

Currently this function always returns @code{nil} on platforms other
Paul Eggert's avatar
Paul Eggert committed
1191
than MS-DOS, MS-Windows, Cygwin, and macOS.  It does not detect
1192
case-insensitivity of mounted filesystems, such as Samba shares or
1193 1194 1195
NFS-mounted Windows volumes.  On remote hosts, it assumes @code{t} for
the @samp{smb} method.  For all other connection methods, runtime
tests are performed.
1196 1197
@end defun

Chong Yidong's avatar
Chong Yidong committed
1198 1199 1200 1201 1202 1203 1204
@defun file-in-directory-p file dir
This function returns @code{t} if @var{file} is a file in directory
@var{dir}, or in a subdirectory of @var{dir}.  It also returns
@code{t} if @var{file} and @var{dir} are the same directory.  It
compares the truenames of the two directories.  If @var{dir} does not
name an existing directory, the return value is @code{nil}.
@end defun
Glenn Morris's avatar
Glenn Morris committed
1205

1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
@defun vc-responsible-backend file
This function determines the responsible VC backend of the given
@var{file}.  For example, if @file{emacs.c} is a file tracked by Git,
@w{@code{(vc-responsible-backend "emacs.c")}} returns @samp{Git}.
Note that if @var{file} is a symbolic link,
@code{vc-responsible-backend} will not resolve it---the backend of the
symbolic link file itself is reported.  To get the backend VC of the
file to which @var{file} refers, wrap @var{file} with a symbolic link
resolving function such as @code{file-chase-links}:

@smallexample
(vc-responsible-backend (file-chase-links "emacs.c"))
@end smallexample
@end defun

Glenn Morris's avatar
Glenn Morris committed
1221
@node File Attributes
Chong Yidong's avatar
Chong Yidong committed
1222 1223
@subsection File Attributes
@cindex file attributes
Glenn Morris's avatar
Glenn Morris committed
1224

1225
  This section describes the functions for getting detailed
Chong Yidong's avatar
Chong Yidong committed
1226 1227 1228
information about a file, including the owner and group numbers, the
number of names, the inode number, the size, and the times of access
and modification.
Glenn Morris's avatar
Glenn Morris committed
1229

Chong Yidong's avatar
Chong Yidong committed
1230 1231 1232 1233 1234 1235 1236
@defun file-newer-than-file-p filename1 filename2
@cindex file age
@cindex file modification time
This function returns @code{t} if the file @var{filename1} is
newer than file @var{filename2}.  If @var{filename1} does not
exist, it returns @code{nil}.  If @var{filename1} does exist, but
@var{filename2} does not, it returns @code{t}.
1237

Chong Yidong's avatar
Chong Yidong committed
1238 1239 1240
In the following example, assume that the file @file{aug-19} was written
on the 19th, @file{aug-20} was written on the 20th, and the file
@file{no-file} doesn't exist at all.
Glenn Morris's avatar
Glenn Morris committed
1241 1242 1243

@example
@group
Chong Yidong's avatar
Chong Yidong committed
1244 1245
(file-newer-than-file-p "aug-19" "aug-20")
     @result{} nil
Glenn Morris's avatar
Glenn Morris committed
1246 1247
@end group
@group
Chong Yidong's avatar
Chong Yidong committed
1248 1249
(file-newer-than-file-p "aug-20" "aug-19")
     @result{} t
Glenn Morris's avatar
Glenn Morris committed
1250 1251
@end group
@group
Chong Yidong's avatar
Chong Yidong committed
1252 1253
(file-newer-than-file-p "aug-19" "no-file")
     @result{} t
Glenn Morris's avatar
Glenn Morris committed
1254 1255
@end group
@group
Chong Yidong's avatar
Chong Yidong committed
1256 1257
(file-newer-than-file-p "no-file" "aug-19")
     @result{} nil
Glenn Morris's avatar
Glenn Morris committed
1258 1259 1260 1261 1262 1263 1264
@end group
@end example
@end defun

@defun file-attributes filename &optional id-format
@anchor{Definition of file-attributes}
This function returns a list of attributes of file @var{filename}.  If
1265 1266
the specified file's attributes cannot be accessed, it returns @code{nil}.
This function does not follow symbolic links.
Glenn Morris's avatar
Glenn Morris committed
1267 1268 1269 1270 1271 1272 1273
The optional parameter @var{id-format} specifies the preferred format
of attributes @acronym{UID} and @acronym{GID} (see below)---the
valid values are @code{'string} and @code{'integer}.  The latter is
the default, but we plan to change that, so you should specify a
non-@code{nil} value for @var{id-format} if you use the returned
@acronym{UID} or @acronym{GID}.

1274 1275 1276 1277
On GNU platforms when operating on a local file, this function is
atomic: if the filesystem is simultaneously being changed by some
other process, this function returns the file's attributes either
before or after the change.  Otherwise this function is not atomic,
1278
and might return @code{nil} if it detects the race condition, or might
1279 1280
return a hodgepodge of the previous and current file attributes.

1281 1282 1283 1284
Accessor functions are provided to access the elements in this list.
The accessors are mentioned along with the descriptions of the
elements below.

Glenn Morris's avatar
Glenn Morris committed
1285 1286 1287 1288 1289
The elements of the list, in order, are:

@enumerate 0
@item
@code{t} for a directory, a string for a symbolic link (the name
1290 1291
linked to), or @code{nil} for a text file
(@code{file-attribute-type}).
Glenn Morris's avatar
Glenn Morris committed
1292 1293 1294

@c Wordy so as to prevent an overfull hbox.  --rjc 15mar92
@item
1295 1296 1297
The number of names the file has (@code{file-attribute-link-number}).
Alternate names, also known as hard links, can be created by using the
@code{add-name-to-file} function (@pxref{Changing Files}).
Glenn Morris's avatar
Glenn Morris committed
1298 1299

@item
1300 1301
The file's @acronym{UID}, normally as a string
(@code{file-attribute-user-id}).  However, if it does not correspond
Paul Eggert's avatar
Paul Eggert committed
1302
to a named user, the value is an integer.
Glenn Morris's avatar
Glenn Morris committed
1303 1304

@item
1305
The file's @acronym{GID}, likewise (@code{file-attribute-group-id}).
Glenn Morris's avatar
Glenn Morris committed
1306 1307

@item
Paul Eggert's avatar
Paul Eggert committed
1308
The time of last access as a Lisp timestamp
1309
(@code{file-attribute-access-time}).  The timestamp is in the
Paul Eggert's avatar
Paul Eggert committed
1310
style of @code{current-time} (@pxref{Time of Day}) and is truncated
Paul Eggert's avatar
Paul Eggert committed
1311
to that of the filesystem's timestamp resolution; for example, on some
1312
FAT-based filesystems, only the date of last access is recorded, so
Eli Zaretskii's avatar
Eli Zaretskii committed
1313
this time will always hold the midnight of the day of the last access.
Glenn Morris's avatar
Glenn Morris committed
1314

1315
@cindex modification time of file
Glenn Morris's avatar
Glenn Morris committed
1316
@item
Paul Eggert's avatar
Paul Eggert committed
1317
The time of last modification as a Lisp timestamp
1318 1319
(@code{file-attribute-modification-time}).  This is the last time when
the file's contents were modified.
Glenn Morris's avatar
Glenn Morris committed
1320 1321

@item
Paul Eggert's avatar
Paul Eggert committed
1322
The time of last status change as a Lisp timestamp
1323
(@code{file-attribute-status-change-time}).  This is the time of the
1324 1325 1326
last change to the file's access mode bits, its owner and group, and
other information recorded in the filesystem for the file, beyond the
file's contents.
Glenn Morris's avatar
Glenn Morris committed
1327 1328

@item
1329
The size of the file in bytes (@code{file-attribute-size}).
Glenn Morris's avatar
Glenn Morris committed
1330 1331

@item
1332 1333
The file's modes, as a string of ten letters or dashes, as in
@samp{ls -l} (@code{file-attribute-modes}).
Glenn Morris's avatar
Glenn Morris committed
1334 1335

@item
1336
An unspecified value, present for backward compatibility.
Glenn Morris's avatar
Glenn Morris committed
1337 1338

@item
Paul Eggert's avatar
Paul Eggert committed
1339 1340
The file's inode number (@code{file-attribute-inode-number}),
a nonnegative integer.
Glenn Morris's avatar
Glenn Morris committed
1341 1342

@item
1343
The filesystem number of the device that the file is on
Paul Eggert's avatar
Paul Eggert committed
1344
@code{file-attribute-device-number}), an integer.
1345
This element and the file's inode number
1346 1347 1348
together give enough information to distinguish any two files on the
system---no two files can have the same values for both of these
numbers.
Glenn Morris's avatar
Glenn Morris committed
1349 1350 1351 1352 1353 1354 1355 1356
@end enumerate

For example, here are the file attributes for @file{files.texi}:

@example
@group
(file-attributes "files.texi" 'string)
     @result{}  (nil 1 "lh" "users"
1357 1358 1359
          (20614 64019 50040 152000)
          (20000 23 0 0)
          (20614 64555 902289 872000)
1360
          122295 "-rw-rw-rw-"
1361 1362
          t 6473924464520138
          1014478468)
Glenn Morris's avatar
Glenn Morris committed
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
@end group
@end example

@noindent
and here is how the result is interpreted:

@table @code
@item nil
is neither a directory nor a symbolic link.

@item 1
has only one name (the name @file{files.texi} in the current default
directory).

@item "lh"
1378
is owned by the user with name @samp{lh}.
Glenn Morris's avatar
Glenn Morris committed
1379 1380

@item "users"
1381
is in the group with name @samp{users}.
Glenn Morris's avatar
Glenn Morris committed
1382

1383 1384
@item (20614 64019 50040 152000)
was last accessed on October 23, 2012, at 20:12:03.050040152 UTC.
Glenn Morris's avatar
Glenn Morris committed
1385

1386 1387
@item (20000 23 0 0)
was last modified on July 15, 2001, at 08:53:43 UTC.
Glenn Morris's avatar
Glenn Morris committed
1388

1389 1390
@item (20614 64555 902289 872000)
last had its status changed on October 23, 2012, at 20:20:59.902289872 UTC.
Glenn Morris's avatar
Glenn Morris committed
1391

1392 1393 1394 1395
@item 122295
is 122295 bytes long.  (It may not contain 122295 characters, though,
if some of the bytes belong to multibyte sequences, and also if the
end-of-line format is CR-LF.)
Glenn Morris's avatar
Glenn Morris committed
1396 1397 1398 1399

@item "-rw-rw-rw-"
has a mode of read and write access for the owner, group, and world.

1400 1401
@item t
is merely a placeholder; it carries no information.
Glenn Morris's avatar
Glenn Morris committed
1402

1403
@item 6473924464520138
1404 1405
has an inode number of 6473924464520138.

1406
@item 1014478468
1407
is on the file-system device whose number is 1014478468.
Glenn Morris's avatar
Glenn Morris committed
1408 1409 1410
@end table
@end defun

Chong Yidong's avatar
Chong Yidong committed
1411 1412 1413 1414 1415
@defun file-nlinks filename
This function returns the number of names (i.e., hard links) that
file @var{filename} has.  If the file does not exist, this function
returns @code{nil}.  Note that symbolic links have no effect on this
function, because they are not considered to be names of the files
1416
they link to.  This function does not follow symbolic links.
1417

Chong Yidong's avatar
Chong Yidong committed
1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433