1@c -*-texinfo-*-
2@c This is part of the GNU Emacs Lisp Reference Manual.
3@c Copyright (C) 1990--1995, 1998--1999, 2001--2021 Free Software
4@c Foundation, Inc.
5@c See the file elisp.texi for copying conditions.
6@node Files
7@chapter Files
8
9  This chapter describes the Emacs Lisp functions and variables to
10find, create, view, save, and otherwise work with files and
11directories.  A few other file-related functions are described in
12@ref{Buffers}, and those related to backups and auto-saving are
13described in @ref{Backups and Auto-Saving}.
14
15  Many of the file functions take one or more arguments that are file
16names.  A file name is a string.  Most of these functions expand file
17name arguments using the function @code{expand-file-name}, so that
18@file{~} is handled correctly, as are relative file names (including
19@file{../} and the empty string).  @xref{File Name Expansion}.
20
21  In addition, certain @dfn{magic} file names are handled specially.
22For example, when a remote file name is specified, Emacs accesses the
23file over the network via an appropriate protocol.  @xref{Remote
24Files,, Remote Files, emacs, The GNU Emacs Manual}.  This handling is
25done at a very low level, so you may assume that all the functions
26described in this chapter accept magic file names as file name
27arguments, except where noted.  @xref{Magic File Names}, for details.
28
29  When file I/O functions signal Lisp errors, they usually use the
30condition @code{file-error} (@pxref{Handling Errors}).  The error
31message is in most cases obtained from the operating system, according
32to locale @code{system-messages-locale}, and decoded using coding system
33@code{locale-coding-system} (@pxref{Locales}).
34
35@menu
36* Visiting Files::           Reading files into Emacs buffers for editing.
37* Saving Buffers::           Writing changed buffers back into files.
38* Reading from Files::       Reading files into buffers without visiting.
39* Writing to Files::         Writing new files from parts of buffers.
40* File Locks::               Locking and unlocking files, to prevent
41                               simultaneous editing by two people.
42* 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
45* File Names::               Decomposing and expanding file names.
46* Contents of Directories::  Getting a list of the files in a directory.
47* Create/Delete Dirs::       Creating and Deleting Directories.
48* Magic File Names::         Special handling for certain file names.
49* Format Conversion::        Conversion to and from various file formats.
50@end menu
51
52@node Visiting Files
53@section Visiting Files
54@cindex finding files
55@cindex visiting files
56
57  Visiting a file means reading a file into a buffer.  Once this is
58done, we say that the buffer is @dfn{visiting} that file, and call the
59file @dfn{the visited file} of the buffer.
60
61  A file and a buffer are two different things.  A file is information
62recorded permanently in the computer (unless you delete it).  A
63buffer, on the other hand, is information inside of Emacs that will
64vanish at the end of the editing session (or when you kill the
65buffer).  When a buffer is visiting a file, it contains information
66copied from the file.  The copy in the buffer is what you modify with
67editing commands.  Changes to the buffer do not change the file; to
68make the changes permanent, you must @dfn{save} the buffer, which
69means copying the altered buffer contents back into the file.
70
71  Despite the distinction between files and buffers, people often
72refer to a file when they mean a buffer and vice-versa.  Indeed, we
73say, ``I am editing a file'', rather than, ``I am editing a buffer
74that I will soon save as a file of the same name''.  Humans do not
75usually need to make the distinction explicit.  When dealing with a
76computer program, however, it is good to keep the distinction in mind.
77
78@menu
79* Visiting Functions::         The usual interface functions for visiting.
80* Subroutines of Visiting::    Lower-level subroutines that they use.
81@end menu
82
83@node Visiting Functions
84@subsection Functions for Visiting Files
85@cindex visiting files, functions for
86@cindex how to visit files
87
88  This section describes the functions normally used to visit files.
89For historical reasons, these functions have names starting with
90@samp{find-} rather than @samp{visit-}.  @xref{Buffer File Name}, for
91functions and variables that access the visited file name of a buffer or
92that find an existing buffer by its visited file name.
93
94  In a Lisp program, if you want to look at the contents of a file but
95not alter it, the fastest way is to use @code{insert-file-contents} in a
96temporary buffer.  Visiting the file is not necessary and takes longer.
97@xref{Reading from Files}.
98
99@deffn Command find-file filename &optional wildcards
100This command selects a buffer visiting the file @var{filename},
101using an existing buffer if there is one, and otherwise creating a
102new buffer and reading the file into it.  It also returns that buffer.
103
104Aside from some technical details, the body of the @code{find-file}
105function is basically equivalent to:
106
107@smallexample
108(switch-to-buffer (find-file-noselect filename nil nil wildcards))
109@end smallexample
110
111@noindent
112(See @code{switch-to-buffer} in @ref{Switching Buffers}.)
113
114If @var{wildcards} is non-@code{nil}, which is always true in an
115interactive call, then @code{find-file} expands wildcard characters in
116@var{filename} and visits all the matching files.
117
118When @code{find-file} is called interactively, it prompts for
119@var{filename} in the minibuffer.
120@end deffn
121
122@deffn Command find-file-literally filename
123This command visits @var{filename}, like @code{find-file} does, but it
124does not perform any format conversions (@pxref{Format Conversion}),
125character code conversions (@pxref{Coding Systems}), or end-of-line
126conversions (@pxref{Coding System Basics, End of line conversion}).
127The buffer visiting the file is made unibyte, and its major mode is
128Fundamental mode, regardless of the file name.  File local variable
129specifications  in the file (@pxref{File Local Variables}) are
130ignored, and automatic decompression and adding a newline at the end
131of the file due to @code{require-final-newline} (@pxref{Saving
132Buffers, require-final-newline}) are also disabled.
133
134Note that if Emacs already has a buffer visiting the same file
135non-literally, it will not visit the same file literally, but instead
136just switch to the existing buffer.  If you want to be sure of
137accessing a file's contents literally, you should create a temporary
138buffer and then read the file contents into it using
139@code{insert-file-contents-literally} (@pxref{Reading from Files}).
140@end deffn
141
142@defun find-file-noselect filename &optional nowarn rawfile wildcards
143This function is the guts of all the file-visiting functions.  It
144returns a buffer visiting the file @var{filename}.  You may make the
145buffer current or display it in a window if you wish, but this
146function does not do so.
147
148The function returns an existing buffer if there is one; otherwise it
149creates a new buffer and reads the file into it.  When
150@code{find-file-noselect} uses an existing buffer, it first verifies
151that the file has not changed since it was last visited or saved in
152that buffer.  If the file has changed, this function asks the user
153whether to reread the changed file.  If the user says @samp{yes}, any
154edits previously made in the buffer are lost.
155
156Reading the file involves decoding the file's contents (@pxref{Coding
157Systems}), including end-of-line conversion, and format conversion
158(@pxref{Format Conversion}).  If @var{wildcards} is non-@code{nil},
159then @code{find-file-noselect} expands wildcard characters in
160@var{filename} and visits all the matching files.
161
162This function displays warning or advisory messages in various peculiar
163cases, unless the optional argument @var{nowarn} is non-@code{nil}.  For
164example, if it needs to create a buffer, and there is no file named
165@var{filename}, it displays the message @samp{(New file)} in the echo
166area, and leaves the buffer empty.
167
168The @code{find-file-noselect} function normally calls
169@code{after-find-file} after reading the file (@pxref{Subroutines of
170Visiting}).  That function sets the buffer major mode, parses local
171variables, warns the user if there exists an auto-save file more recent
172than the file just visited, and finishes by running the functions in
173@code{find-file-hook}.
174
175If the optional argument @var{rawfile} is non-@code{nil}, then
176@code{after-find-file} is not called, and the
177@code{find-file-not-found-functions} are not run in case of failure.
178What's more, a non-@code{nil} @var{rawfile} value suppresses coding
179system conversion and format conversion.
180
181The @code{find-file-noselect} function usually returns the buffer that
182is visiting the file @var{filename}.  But, if wildcards are actually
183used and expanded, it returns a list of buffers that are visiting the
184various files.
185
186@example
187@group
188(find-file-noselect "/etc/fstab")
189     @result{} #<buffer fstab>
190@end group
191@end example
192@end defun
193
194@deffn Command find-file-other-window filename &optional wildcards
195This command selects a buffer visiting the file @var{filename}, but
196does so in a window other than the selected window.  It may use
197another existing window or split a window; see @ref{Switching
198Buffers}.
199
200When this command is called interactively, it prompts for
201@var{filename}.
202@end deffn
203
204@deffn Command find-file-read-only filename &optional wildcards
205This command selects a buffer visiting the file @var{filename}, like
206@code{find-file}, but it marks the buffer as read-only.  @xref{Read Only
207Buffers}, for related functions and variables.
208
209When this command is called interactively, it prompts for
210@var{filename}.
211@end deffn
212
213@defopt find-file-wildcards
214If this variable is non-@code{nil}, then the various @code{find-file}
215commands check for wildcard characters and visit all the files that
216match them (when invoked interactively or when their @var{wildcards}
217argument is non-@code{nil}).  If this option is @code{nil}, then
218the @code{find-file} commands ignore their @var{wildcards} argument
219and never treat wildcard characters specially.
220@end defopt
221
222@defopt find-file-hook
223The value of this variable is a list of functions to be called after a
224file is visited.  The file's local-variables specification (if any) will
225have been processed before the hooks are run.  The buffer visiting the
226file is current when the hook functions are run.
227
228This variable is a normal hook.  @xref{Hooks}.
229@end defopt
230
231@defvar find-file-not-found-functions
232The value of this variable is a list of functions to be called when
233@code{find-file} or @code{find-file-noselect} is passed a nonexistent
234file name.  @code{find-file-noselect} calls these functions as soon as
235it detects a nonexistent file.  It calls them in the order of the list,
236until one of them returns non-@code{nil}.  @code{buffer-file-name} is
237already set up.
238
239This is not a normal hook because the values of the functions are
240used, and in many cases only some of the functions are called.
241@end defvar
242
243@defvar find-file-literally
244This buffer-local variable, if set to a non-@code{nil} value, makes
245@code{save-buffer} behave as if the buffer were visiting its file
246literally, i.e., without conversions of any kind.  The command
247@code{find-file-literally} sets this variable's local value, but other
248equivalent functions and commands can do that as well, e.g., to avoid
249automatic addition of a newline at the end of the file.  This variable
250is permanent local, so it is unaffected by changes of major modes.
251@end defvar
252
253@node Subroutines of Visiting
254@subsection Subroutines of Visiting
255
256  The @code{find-file-noselect} function uses two important subroutines
257which are sometimes useful in user Lisp code: @code{create-file-buffer}
258and @code{after-find-file}.  This section explains how to use them.
259
260@c FIXME This does not describe the default behavior, because
261@c uniquify is enabled by default and advises this function.
262@c This is confusing.  uniquify should be folded into the function proper.
263@defun create-file-buffer filename
264This function creates a suitably named buffer for visiting
265@var{filename}, and returns it.  It uses @var{filename} (sans directory)
266as the name if that name is free; otherwise, it appends a string such as
267@samp{<2>} to get an unused name.  See also @ref{Creating Buffers}.
268Note that the @file{uniquify} library affects the result of this
269function.  @xref{Uniquify,,, emacs, The GNU Emacs Manual}.
270
271@strong{Please note:} @code{create-file-buffer} does @emph{not}
272associate the new buffer with a file and does not select the buffer.
273It also does not use the default major mode.
274
275@example
276@group
277(create-file-buffer "foo")
278     @result{} #<buffer foo>
279@end group
280@group
281(create-file-buffer "foo")
282     @result{} #<buffer foo<2>>
283@end group
284@group
285(create-file-buffer "foo")
286     @result{} #<buffer foo<3>>
287@end group
288@end example
289
290This function is used by @code{find-file-noselect}.
291It uses @code{generate-new-buffer} (@pxref{Creating Buffers}).
292@end defun
293
294@defun after-find-file &optional error warn noauto after-find-file-from-revert-buffer nomodes
295This function sets the buffer major mode, and parses local variables
296(@pxref{Auto Major Mode}).  It is called by @code{find-file-noselect}
297and by the default revert function (@pxref{Reverting}).
298
299@cindex new file message
300@cindex file open error
301If reading the file got an error because the file does not exist, but
302its directory does exist, the caller should pass a non-@code{nil} value
303for @var{error}.  In that case, @code{after-find-file} issues a warning:
304@samp{(New file)}.  For more serious errors, the caller should usually not
305call @code{after-find-file}.
306
307If @var{warn} is non-@code{nil}, then this function issues a warning
308if an auto-save file exists and is more recent than the visited file.
309
310If @var{noauto} is non-@code{nil}, that says not to enable or disable
311Auto-Save mode.  The mode remains enabled if it was enabled before.
312
313If @var{after-find-file-from-revert-buffer} is non-@code{nil}, that
314means this call was from @code{revert-buffer}.  This has no direct
315effect, but some mode functions and hook functions check the value
316of this variable.
317
318If @var{nomodes} is non-@code{nil}, that means don't alter the buffer's
319major mode, don't process local variables specifications in the file,
320and don't run @code{find-file-hook}.  This feature is used by
321@code{revert-buffer} in some cases.
322
323The last thing @code{after-find-file} does is call all the functions
324in the list @code{find-file-hook}.
325@end defun
326
327@node Saving Buffers
328@section Saving Buffers
329@cindex saving buffers
330
331  When you edit a file in Emacs, you are actually working on a buffer
332that is visiting that file---that is, the contents of the file are
333copied into the buffer and the copy is what you edit.  Changes to the
334buffer do not change the file until you @dfn{save} the buffer, which
335means copying the contents of the buffer into the file.  Buffers which
336are not visiting a file can still be ``saved'', in a sense, using
337functions in the buffer-local @code{write-contents-functions} hook.
338
339@deffn Command save-buffer &optional backup-option
340This function saves the contents of the current buffer in its visited
341file if the buffer has been modified since it was last visited or saved.
342Otherwise it does nothing.
343
344@code{save-buffer} is responsible for making backup files.  Normally,
345@var{backup-option} is @code{nil}, and @code{save-buffer} makes a backup
346file only if this is the first save since visiting the file.  Other
347values for @var{backup-option} request the making of backup files in
348other circumstances:
349
350@itemize @bullet
351@item
352With an argument of 4 or 64, reflecting 1 or 3 @kbd{C-u}'s, the
353@code{save-buffer} function marks this version of the file to be
354backed up when the buffer is next saved.
355
356@item
357With an argument of 16 or 64, reflecting 2 or 3 @kbd{C-u}'s, the
358@code{save-buffer} function unconditionally backs up the previous
359version of the file before saving it.
360
361@item
362With an argument of 0, unconditionally do @emph{not} make any backup file.
363@end itemize
364@end deffn
365
366@deffn Command save-some-buffers &optional save-silently-p pred
367@anchor{Definition of save-some-buffers}
368This command saves some modified file-visiting buffers.  Normally it
369asks the user about each buffer.  But if @var{save-silently-p} is
370non-@code{nil}, it saves all the file-visiting buffers without
371querying the user.
372
373@vindex save-some-buffers-default-predicate
374The optional @var{pred} argument provides a predicate that controls
375which buffers to ask about (or to save silently if
376@var{save-silently-p} is non-@code{nil}).  If @var{pred} is
377@code{nil}, that means to use the value of
378@code{save-some-buffers-default-predicate} instead of @var{pred}.  If
379the result is @code{nil}, it means ask only about file-visiting
380buffers.  If it is @code{t}, that means also offer to save certain
381other non-file buffers---those that have a non-@code{nil} buffer-local
382value of @code{buffer-offer-save} (@pxref{Killing Buffers}).  A user
383who says @samp{yes} to saving a non-file buffer is asked to specify
384the file name to use.  The @code{save-buffers-kill-emacs} function
385passes the value @code{t} for @var{pred}.
386
387If the predicate is neither @code{t} nor @code{nil}, then it should be
388a function of no arguments.  It will be called in each buffer to decide
389whether to offer to save that buffer.  If it returns a non-@code{nil}
390value in a certain buffer, that means do offer to save that buffer.
391@end deffn
392
393@deffn Command write-file filename &optional confirm
394@anchor{Definition of write-file}
395This function writes the current buffer into file @var{filename}, makes
396the buffer visit that file, and marks it not modified.  Then it renames
397the buffer based on @var{filename}, appending a string like @samp{<2>}
398if necessary to make a unique buffer name.  It does most of this work by
399calling @code{set-visited-file-name} (@pxref{Buffer File Name}) and
400@code{save-buffer}.
401
402If @var{confirm} is non-@code{nil}, that means to ask for confirmation
403before overwriting an existing file.  Interactively, confirmation is
404required, unless the user supplies a prefix argument.
405
406If @var{filename} is a directory name (@pxref{Directory Names}),
407@code{write-file} uses the name of the visited file, in directory
408@var{filename}.  If the buffer is not visiting a file, it uses the
409buffer name instead.
410@end deffn
411
412  Saving a buffer runs several hooks.  It also performs format
413conversion (@pxref{Format Conversion}).  Note that these hooks,
414described below, are only run by @code{save-buffer}, they are not run
415by other primitives and functions that write buffer text to files, and
416in particular auto-saving (@pxref{Auto-Saving}) doesn't run these
417hooks.
418
419@defvar write-file-functions
420The value of this variable is a list of functions to be called before
421writing out a buffer to its visited file.  If one of them returns
422non-@code{nil}, the file is considered already written and the rest of
423the functions are not called, nor is the usual code for writing the file
424executed.
425
426If a function in @code{write-file-functions} returns non-@code{nil}, it
427is responsible for making a backup file (if that is appropriate).
428To do so, execute the following code:
429
430@example
431(or buffer-backed-up (backup-buffer))
432@end example
433
434You might wish to save the file modes value returned by
435@code{backup-buffer} and use that (if non-@code{nil}) to set the mode
436bits of the file that you write.  This is what @code{save-buffer}
437normally does.  @xref{Making Backups,, Making Backup Files}.
438
439The hook functions in @code{write-file-functions} are also responsible
440for encoding the data (if desired): they must choose a suitable coding
441system and end-of-line conversion (@pxref{Lisp and Coding Systems}),
442perform the encoding (@pxref{Explicit Encoding}), and set
443@code{last-coding-system-used} to the coding system that was used
444(@pxref{Encoding and I/O}).
445
446If you set this hook locally in a buffer, it is assumed to be
447associated with the file or the way the contents of the buffer were
448obtained.  Thus the variable is marked as a permanent local, so that
449changing the major mode does not alter a buffer-local value.  On the
450other hand, calling @code{set-visited-file-name} will reset it.
451If this is not what you want, you might like to use
452@code{write-contents-functions} instead.
453
454Even though this is not a normal hook, you can use @code{add-hook} and
455@code{remove-hook} to manipulate the list.  @xref{Hooks}.
456@end defvar
457
458@c Emacs 19 feature
459@defvar write-contents-functions
460This works just like @code{write-file-functions}, but it is intended
461for hooks that pertain to the buffer's contents, not to the particular
462visited file or its location, and can be used to create arbitrary save
463processes for buffers that aren't visiting files at all.  Such hooks
464are usually set up by major modes, as buffer-local bindings for this
465variable.  This variable automatically becomes buffer-local whenever
466it is set; switching to a new major mode always resets this variable,
467but calling @code{set-visited-file-name} does not.
468
469If any of the functions in this hook returns non-@code{nil}, the file
470is considered already written and the rest are not called and neither
471are the functions in @code{write-file-functions}.
472
473When using this hook to save buffers that are not visiting files (for
474instance, special-mode buffers), keep in mind that, if the function
475fails to save correctly and returns a @code{nil} value,
476@code{save-buffer} will go on to prompt the user for a file to save
477the buffer in.  If this is undesirable, consider having the function
478fail by raising an error.
479@end defvar
480
481@defopt before-save-hook
482This normal hook runs before a buffer is saved in its visited file,
483regardless of whether that is done normally or by one of the hooks
484described above.  For instance, the @file{copyright.el} program uses
485this hook to make sure the file you are saving has the current year in
486its copyright notice.
487@end defopt
488
489@c Emacs 19 feature
490@defopt after-save-hook
491This normal hook runs after a buffer has been saved in its visited file.
492@end defopt
493
494@defopt file-precious-flag
495If this variable is non-@code{nil}, then @code{save-buffer} protects
496against I/O errors while saving by writing the new file to a temporary
497name instead of the name it is supposed to have, and then renaming it to
498the intended name after it is clear there are no errors.  This procedure
499prevents problems such as a lack of disk space from resulting in an
500invalid file.
501
502As a side effect, backups are necessarily made by copying.  @xref{Rename
503or Copy}.  Yet, at the same time, saving a precious file always breaks
504all hard links between the file you save and other file names.
505
506Some modes give this variable a non-@code{nil} buffer-local value
507in particular buffers.
508@end defopt
509
510@defopt require-final-newline
511This variable determines whether files may be written out that do
512@emph{not} end with a newline.  If the value of the variable is
513@code{t}, then @code{save-buffer} silently adds a newline at the end
514of the buffer whenever it does not already end in one.  If the value
515is @code{visit}, Emacs adds a missing newline just after it visits the
516file.  If the value is @code{visit-save}, Emacs adds a missing newline
517both on visiting and on saving.  For any other non-@code{nil} value,
518@code{save-buffer} asks the user whether to add a newline each time
519the case arises.
520
521If the value of the variable is @code{nil}, then @code{save-buffer}
522doesn't add newlines at all.  @code{nil} is the default value, but a few
523major modes set it to @code{t} in particular buffers.
524@end defopt
525
526  See also the function @code{set-visited-file-name} (@pxref{Buffer File
527Name}).
528
529@node Reading from Files
530@section Reading from Files
531@cindex reading from files
532
533  To copy the contents of a file into a buffer, use the function
534@code{insert-file-contents}.  (Don't use the command
535@code{insert-file} in a Lisp program, as that sets the mark.)
536
537@defun insert-file-contents filename &optional visit beg end replace
538This function inserts the contents of file @var{filename} into the
539current buffer after point.  It returns a list of the absolute file name
540and the length of the data inserted.  An error is signaled if
541@var{filename} is not the name of a file that can be read.
542
543This function checks the file contents against the defined file
544formats, and converts the file contents if appropriate and also calls
545the functions in the list @code{after-insert-file-functions}.
546@xref{Format Conversion}.  Normally, one of the functions in the
547@code{after-insert-file-functions} list determines the coding system
548(@pxref{Coding Systems}) used for decoding the file's contents,
549including end-of-line conversion.  However, if the file contains null
550bytes, it is by default visited without any code conversions.
551@xref{Lisp and Coding Systems, inhibit-nul-byte-detection}.
552
553If @var{visit} is non-@code{nil}, this function additionally marks the
554buffer as unmodified and sets up various fields in the buffer so that it
555is visiting the file @var{filename}: these include the buffer's visited
556file name and its last save file modtime.  This feature is used by
557@code{find-file-noselect} and you probably should not use it yourself.
558
559If @var{beg} and @var{end} are non-@code{nil}, they should be numbers
560that are byte offsets specifying the portion of the file to insert.
561In this case, @var{visit} must be @code{nil}.  For example,
562
563@example
564(insert-file-contents filename nil 0 500)
565@end example
566
567@noindent
568inserts the first 500 characters of a file.
569
570If the argument @var{replace} is non-@code{nil}, it means to replace the
571contents of the buffer (actually, just the accessible portion) with the
572contents of the file.  This is better than simply deleting the buffer
573contents and inserting the whole file, because (1) it preserves some
574marker positions and (2) it puts less data in the undo list.
575
576It is possible to read a special file (such as a FIFO or an I/O device)
577with @code{insert-file-contents}, as long as @var{replace} and
578@var{visit} are @code{nil}.
579@end defun
580
581@defun insert-file-contents-literally filename &optional visit beg end replace
582This function works like @code{insert-file-contents} except that it
583does not run @code{after-insert-file-functions}, and does not do
584format decoding, character code conversion, automatic uncompression,
585and so on.
586@end defun
587
588If you want to pass a file name to another process so that another
589program can read the file, use the function @code{file-local-copy}; see
590@ref{Magic File Names}.
591
592@node Writing to Files
593@section Writing to Files
594@cindex writing to files
595
596  You can write the contents of a buffer, or part of a buffer, directly
597to a file on disk using the @code{append-to-file} and
598@code{write-region} functions.  Don't use these functions to write to
599files that are being visited; that could cause confusion in the
600mechanisms for visiting.
601
602@deffn Command append-to-file start end filename
603This function appends the contents of the region delimited by
604@var{start} and @var{end} in the current buffer to the end of file
605@var{filename}.  If that file does not exist, it is created.  This
606function returns @code{nil}.
607
608An error is signaled if you cannot write or create @var{filename}.
609
610When called from Lisp, this function is completely equivalent to:
611
612@example
613(write-region start end filename t)
614@end example
615@end deffn
616
617@deffn Command write-region start end filename &optional append visit lockname mustbenew
618This function writes the region delimited by @var{start} and @var{end}
619in the current buffer into the file specified by @var{filename}.
620
621If @var{start} is @code{nil}, then the command writes the entire buffer
622contents (@emph{not} just the accessible portion) to the file and
623ignores @var{end}.
624
625@c Emacs 19 feature
626If @var{start} is a string, then @code{write-region} writes or appends
627that string, rather than text from the buffer.  @var{end} is ignored in
628this case.
629
630If @var{append} is non-@code{nil}, then the specified text is appended
631to the existing file contents (if any).  If @var{append} is a
632number, @code{write-region} seeks to that byte offset from the start
633of the file and writes the data from there.
634
635If @var{mustbenew} is non-@code{nil}, then @code{write-region} asks
636for confirmation if @var{filename} names an existing file.  If
637@var{mustbenew} is the symbol @code{excl}, then @code{write-region}
638does not ask for confirmation, but instead it signals an error
639@code{file-already-exists} if the file already exists.  Although
640@code{write-region} normally follows a symbolic link and creates the
641pointed-to file if the symbolic link is dangling, it does not follow
642symbolic links if @var{mustbenew} is @code{excl}.
643
644The test for an existing file, when @var{mustbenew} is @code{excl}, uses
645a special system feature.  At least for files on a local disk, there is
646no chance that some other program could create a file of the same name
647before Emacs does, without Emacs's noticing.
648
649If @var{visit} is @code{t}, then Emacs establishes an association
650between the buffer and the file: the buffer is then visiting that file.
651It also sets the last file modification time for the current buffer to
652@var{filename}'s modtime, and marks the buffer as not modified.  This
653feature is used by @code{save-buffer}, but you probably should not use
654it yourself.
655
656@c Emacs 19 feature
657If @var{visit} is a string, it specifies the file name to visit.  This
658way, you can write the data to one file (@var{filename}) while recording
659the buffer as visiting another file (@var{visit}).  The argument
660@var{visit} is used in the echo area message and also for file locking;
661@var{visit} is stored in @code{buffer-file-name}.  This feature is used
662to implement @code{file-precious-flag}; don't use it yourself unless you
663really know what you're doing.
664
665The optional argument @var{lockname}, if non-@code{nil}, specifies the
666file name to use for purposes of locking and unlocking, overriding
667@var{filename} and @var{visit} for that purpose.
668
669The function @code{write-region} converts the data which it writes to
670the appropriate file formats specified by @code{buffer-file-format}
671and also calls the functions in the list
672@code{write-region-annotate-functions}.
673@xref{Format Conversion}.
674
675Normally, @code{write-region} displays the message @samp{Wrote
676@var{filename}} in the echo area.  This message is inhibited if
677@var{visit} is neither @code{t} nor @code{nil} nor a string, or if
678Emacs is operating in batch mode (@pxref{Batch Mode}).  This
679feature is useful for programs that use files for internal purposes,
680files that the user does not need to know about.
681@end deffn
682
683@defvar write-region-inhibit-fsync
684If this variable's value is @code{nil}, @code{write-region} uses the
685@code{fsync} system call after writing a file.  Although this slows
686Emacs down, it lessens the risk of data loss after power failure.  If
687the value is @code{t}, Emacs does not use @code{fsync}.  The default
688value is @code{nil} when Emacs is interactive, and @code{t} when Emacs
689runs in batch mode.  @xref{Files and Storage}.
690@end defvar
691
692@defmac with-temp-file file body@dots{}
693@anchor{Definition of with-temp-file}
694The @code{with-temp-file} macro evaluates the @var{body} forms with a
695temporary buffer as the current buffer; then, at the end, it writes the
696buffer contents into file @var{file}.  It kills the temporary buffer
697when finished, restoring the buffer that was current before the
698@code{with-temp-file} form.  Then it returns the value of the last form
699in @var{body}.
700
701The current buffer is restored even in case of an abnormal exit via
702@code{throw} or error (@pxref{Nonlocal Exits}).
703
704See also @code{with-temp-buffer} in @ref{Definition of
705with-temp-buffer,, The Current Buffer}.
706@end defmac
707
708@node File Locks
709@section File Locks
710@cindex file locks
711@cindex lock file
712@cindex .#, lock file names
713
714  When two users edit the same file at the same time, they are likely
715to interfere with each other.  Emacs tries to prevent this situation
716from arising by recording a @dfn{file lock} when a file is being
717modified.
718Emacs can then detect the first attempt to modify a buffer visiting a
719file that is locked by another Emacs job, and ask the user what to do.
720The file lock is really a file, a symbolic link with a special name,
721stored in the same directory as the file you are editing.  The name is
722constructed by prepending @file{.#} to the filename of the buffer.
723The target of the symbolic link will be of the form
724@code{@var{user}@@@var{host}.@var{pid}:@var{boot}}, where @var{user}
725is replaced with the current username (from @code{user-login-name}),
726@var{host} with the name of the host where Emacs is running (from
727@code{system-name}), @var{pid} with Emacs's process id, and @var{boot}
728with the time since the last reboot.  @code{:@var{boot}} is omitted if
729the boot time is unavailable.  (On file systems that do not support
730symbolic links, a regular file is used instead, with contents of the
731form @code{@var{user}@@@var{host}.@var{pid}:@var{boot}}.)
732
733  When you access files using NFS, there may be a small probability that
734you and another user will both lock the same file simultaneously.
735If this happens, it is possible for the two users to make changes
736simultaneously, but Emacs will still warn the user who saves second.
737Also, the detection of modification of a buffer visiting a file changed
738on disk catches some cases of simultaneous editing; see
739@ref{Modification Time}.
740
741@defun file-locked-p filename
742This function returns @code{nil} if the file @var{filename} is not
743locked.  It returns @code{t} if it is locked by this Emacs process, and
744it returns the name of the user who has locked it if it is locked by
745some other job.
746
747@example
748@group
749(file-locked-p "foo")
750     @result{} nil
751@end group
752@end example
753@end defun
754
755@defun lock-buffer &optional filename
756This function locks the file @var{filename}, if the current buffer is
757modified.  The argument @var{filename} defaults to the current buffer's
758visited file.  Nothing is done if the current buffer is not visiting a
759file, or is not modified, or if the option @code{create-lockfiles} is
760@code{nil}.
761@end defun
762
763@defun unlock-buffer
764This function unlocks the file being visited in the current buffer,
765if the buffer is modified.  If the buffer is not modified, then
766the file should not be locked, so this function does nothing.  It also
767does nothing if the current buffer is not visiting a file, or is not locked.
768@end defun
769
770@defopt create-lockfiles
771If this variable is @code{nil}, Emacs does not lock files.
772@end defopt
773
774@defun ask-user-about-lock file other-user
775This function is called when the user tries to modify @var{file}, but it
776is locked by another user named @var{other-user}.  The default
777definition of this function asks the user to say what to do.  The value
778this function returns determines what Emacs does next:
779
780@itemize @bullet
781@item
782A value of @code{t} says to grab the lock on the file.  Then
783this user may edit the file and @var{other-user} loses the lock.
784
785@item
786A value of @code{nil} says to ignore the lock and let this
787user edit the file anyway.
788
789@item
790@kindex file-locked
791This function may instead signal a @code{file-locked} error, in which
792case the change that the user was about to make does not take place.
793
794The error message for this error looks like this:
795
796@example
797@error{} File is locked: @var{file} @var{other-user}
798@end example
799
800@noindent
801where @code{file} is the name of the file and @var{other-user} is the
802name of the user who has locked the file.
803@end itemize
804
805If you wish, you can replace the @code{ask-user-about-lock} function
806with your own version that makes the decision in another way.
807@end defun
808
809@node Information about Files
810@section Information about Files
811@cindex file, information about
812
813  This section describes the functions for retrieving various types of
814information about files (or directories or symbolic links), such as
815whether a file is readable or writable, and its size.  These functions
816all take arguments which are file names.  Except where noted, these
817arguments need to specify existing files, or an error is signaled.
818
819@cindex file names, trailing whitespace
820@cindex trailing blanks in file names
821  Be careful with file names that end in spaces.  On some filesystems
822(notably, MS-Windows), trailing whitespace characters in file names
823are silently and automatically ignored.
824
825@menu
826* Testing Accessibility::   Is a given file readable?  Writable?
827* Kinds of Files::          Is it a directory?  A symbolic link?
828* Truenames::               Eliminating symbolic links from a file name.
829* File Attributes::         File sizes, modification times, etc.
830* Extended Attributes::     Extended file attributes for access control.
831* Locating Files::          How to find a file in standard places.
832@end menu
833
834@node Testing Accessibility
835@subsection Testing Accessibility
836@cindex accessibility of a file
837@cindex file accessibility
838
839  These functions test for permission to access a file for reading,
840writing, or execution.  Unless explicitly stated otherwise, they
841follow symbolic links.  @xref{Kinds of Files}.
842
843  On some operating systems, more complex sets of access permissions
844can be specified, via mechanisms such as Access Control Lists (ACLs).
845@xref{Extended Attributes}, for how to query and set those
846permissions.
847
848@defun file-exists-p filename
849This function returns @code{t} if a file named @var{filename} appears
850to exist.  This does not mean you can necessarily read the file, only
851that you can probably find out its attributes.  (On GNU and other POSIX-like
852systems, this is true if the file exists and you have execute
853permission on the containing directories, regardless of the
854permissions of the file itself.)
855
856If the file does not exist, or if there was trouble determining
857whether the file exists, this function returns @code{nil}.
858
859Directories are files, so @code{file-exists-p} can return @code{t} when
860given a directory.  However, because @code{file-exists-p} follows
861symbolic links, it returns @code{t} for a symbolic link
862name only if the target file exists.
863@end defun
864
865@defun file-readable-p filename
866This function returns @code{t} if a file named @var{filename} exists
867and you can read it.  It returns @code{nil} otherwise.
868@end defun
869
870@defun file-executable-p filename
871This function returns @code{t} if a file named @var{filename} exists
872and you can execute it.  It returns @code{nil} otherwise.  On GNU and
873other POSIX-like systems, if the file is a directory, execute
874permission means you can check the existence and attributes of files
875inside the directory, and open those files if their modes permit.
876@end defun
877
878@defun file-writable-p filename
879This function returns @code{t} if the file @var{filename} can be written
880or created by you, and @code{nil} otherwise.  A file is writable if the
881file exists and you can write it.  It is creatable if it does not exist,
882but its parent directory does exist and you can write in that
883directory.
884
885In the example below, @file{foo} is not writable because the parent
886directory does not exist, even though the user could create such a
887directory.
888
889@example
890@group
891(file-writable-p "~/no-such-dir/foo")
892     @result{} nil
893@end group
894@end example
895@end defun
896
897@defun file-accessible-directory-p dirname
898This function returns @code{t} if you have permission to open existing
899files in the directory whose name as a file is @var{dirname};
900otherwise (e.g., if there is no such directory), it returns @code{nil}.
901The value of @var{dirname} may be either a directory name (such as
902@file{/foo/}) or the file name of a file which is a directory
903(such as @file{/foo}, without the final slash).
904
905For example, from the following we deduce that any attempt to read a
906file in @file{/foo/} will give an error:
907
908@example
909(file-accessible-directory-p "/foo")
910     @result{} nil
911@end example
912@end defun
913
914@defun access-file filename string
915If you can read @var{filename} this function returns @code{nil};
916otherwise it signals an error
917using @var{string} as the error message text.
918@end defun
919
920@defun file-ownership-preserved-p filename &optional group
921This function returns @code{t} if deleting the file @var{filename} and
922then creating it anew would keep the file's owner unchanged.  It also
923returns @code{t} for nonexistent files.
924
925If the optional argument @var{group} is non-@code{nil}, this function
926also checks that the file's group would be unchanged.
927
928This function does not follow symbolic links.
929@end defun
930
931@defun file-modes filename
932@cindex mode bits
933@cindex file permissions
934@cindex permissions, file
935@cindex file modes
936This function returns the @dfn{mode bits} of @var{filename}---an
937integer summarizing its read, write, and execution permissions.
938This function follows symbolic links.  If the file does not exist, the
939return value is @code{nil}.
940
941@xref{File permissions,,, coreutils, The @sc{gnu} @code{Coreutils}
942Manual}, for a description of mode bits.  For example, if the
943low-order bit is 1, the file is executable by all users; if the
944second-lowest-order bit is 1, the file is writable by all users; etc.
945The highest possible value is 4095 (7777 octal), meaning that everyone
946has read, write, and execute permission, the @acronym{SUID} bit is set
947for both others and group, and the sticky bit is set.
948
949@xref{Changing Files}, for the @code{set-file-modes} function, which
950can be used to set these permissions.
951
952@example
953@group
954(file-modes "~/junk/diffs")
955     @result{} 492               ; @r{Decimal integer.}
956@end group
957@group
958(format "%o" 492)
959     @result{} "754"             ; @r{Convert to octal.}
960@end group
961
962@group
963(set-file-modes "~/junk/diffs" #o666)
964     @result{} nil
965@end group
966
967@group
968$ ls -l diffs
969-rw-rw-rw- 1 lewis lewis 3063 Oct 30 16:00 diffs
970@end group
971@end example
972
973@cindex MS-DOS and file modes
974@cindex file modes and MS-DOS
975@strong{MS-DOS note:} On MS-DOS, there is no such thing as an
976executable file mode bit.  So @code{file-modes} considers a file
977executable if its name ends in one of the standard executable
978extensions, such as @file{.com}, @file{.bat}, @file{.exe}, and some
979others.  Files that begin with the POSIX-standard @samp{#!} signature,
980such as shell and Perl scripts, are also considered executable.
981Directories are also reported as executable, for compatibility with
982POSIX@.  These conventions are also followed by @code{file-attributes}
983(@pxref{File Attributes}).
984@end defun
985
986@node Kinds of Files
987@subsection Distinguishing Kinds of Files
988@cindex file classification
989@cindex classification of file types
990@cindex symbolic links
991
992  This section describes how to distinguish various kinds of files, such
993as directories, symbolic links, and ordinary files.
994
995  Symbolic links are ordinarily followed wherever they appear.  For
996example, to interpret the file name @file{a/b/c}, any of @file{a},
997@file{a/b}, and @file{a/b/c} can be symbolic links that are followed,
998possibly recursively if the link targets are themselves symbolic
999links.  However, a few functions do not follow symbolic links at the
1000end of a file name (@file{a/b/c} in this example).  Such a function
1001is said to @dfn{not follow symbolic links}.
1002
1003@defun file-symlink-p filename
1004@cindex symbolic links
1005If the file @var{filename} is a symbolic link, this function does not
1006follow it and instead returns its link target
1007as a string.  (The link target string is not necessarily the full
1008absolute file name of the target; determining the full file name that
1009the link points to is nontrivial, see below.)
1010
1011If the file @var{filename} is not a symbolic link, or does not exist,
1012or if there is trouble determining whether it is a symbolic link,
1013@code{file-symlink-p} returns @code{nil}.
1014
1015Here are a few examples of using this function:
1016
1017@example
1018@group
1019(file-symlink-p "not-a-symlink")
1020     @result{} nil
1021@end group
1022@group
1023(file-symlink-p "sym-link")
1024     @result{} "not-a-symlink"
1025@end group
1026@group
1027(file-symlink-p "sym-link2")
1028     @result{} "sym-link"
1029@end group
1030@group
1031(file-symlink-p "/bin")
1032     @result{} "/pub/bin"
1033@end group
1034@end example
1035
1036Note that in the third example, the function returned @file{sym-link},
1037but did not proceed to resolve it, although that file is itself a
1038symbolic link.  That is because this function does not follow symbolic
1039links---the process of following the symbolic links does not apply to
1040the last component of the file name.
1041
1042The string that this function returns is what is recorded in the
1043symbolic link; it may or may not include any leading directories.
1044This function does @emph{not} expand the link target to produce a
1045fully-qualified file name, and in particular does not use the leading
1046directories, if any, of the @var{filename} argument if the link target
1047is not an absolute file name.  Here's an example:
1048
1049@example
1050@group
1051(file-symlink-p "/foo/bar/baz")
1052     @result{} "some-file"
1053@end group
1054@end example
1055
1056@noindent
1057Here, although @file{/foo/bar/baz} was given as a fully-qualified file
1058name, the result is not, and in fact does not have any leading
1059directories at all.  And since @file{some-file} might itself be a
1060symbolic link, you cannot simply prepend leading directories to it,
1061nor even naively use @code{expand-file-name} (@pxref{File Name
1062Expansion}) to produce its absolute file name.
1063
1064For this reason, this function is seldom useful if you need to
1065determine more than just the fact that a file is or isn't a symbolic
1066link.  If you actually need the file name of the link target, use
1067@code{file-chase-links} or @code{file-truename}, described in
1068@ref{Truenames}.
1069@end defun
1070
1071@defun file-directory-p filename
1072This function returns @code{t} if @var{filename} is the name of an
1073existing directory.  It returns @code{nil} if @var{filename} does
1074not name a directory, or if there is trouble determining whether
1075it is a directory.
1076This function follows symbolic links.
1077
1078@example
1079@group
1080(file-directory-p "~rms")
1081     @result{} t
1082@end group
1083@group
1084(file-directory-p "~rms/lewis/files.texi")
1085     @result{} nil
1086@end group
1087@group
1088(file-directory-p "~rms/lewis/no-such-file")
1089     @result{} nil
1090@end group
1091@group
1092(file-directory-p "$HOME")
1093     @result{} nil
1094@end group
1095@group
1096(file-directory-p
1097 (substitute-in-file-name "$HOME"))
1098     @result{} t
1099@end group
1100@end example
1101@end defun
1102
1103@defun file-regular-p filename
1104This function returns @code{t} if the file @var{filename} exists and is
1105a regular file (not a directory, named pipe, terminal, or
1106other I/O device).
1107It returns @code{nil} if @var{filename} does not exist or is not a regular
1108file, or if there is trouble determining whether it is a regular file.
1109This function follows symbolic links.
1110@end defun
1111
1112@node Truenames
1113@subsection Truenames
1114@cindex truename (of file)
1115
1116  The @dfn{truename} of a file is the name that you get by following
1117symbolic links at all levels until none remain, then simplifying away
1118@samp{.}@: and @samp{..}@: appearing as name components.  This results
1119in a sort of canonical name for the file.  A file does not always have a
1120unique truename; the number of distinct truenames a file has is equal to
1121the number of hard links to the file.  However, truenames are useful
1122because they eliminate symbolic links as a cause of name variation.
1123
1124@defun file-truename filename
1125This function returns the truename of the file @var{filename}.  If the
1126argument is not an absolute file name, this function first expands it
1127against @code{default-directory}.
1128
1129This function does not expand environment variables.  Only
1130@code{substitute-in-file-name} does that.  @xref{Definition of
1131substitute-in-file-name}.
1132
1133If you may need to follow symbolic links preceding @samp{..}@:
1134appearing as a name component, call @code{file-truename} without prior
1135direct or indirect calls to @code{expand-file-name}.  Otherwise, the
1136file name component immediately preceding @samp{..} will be
1137simplified away before @code{file-truename} is called.  To
1138eliminate the need for a call to @code{expand-file-name},
1139@code{file-truename} handles @samp{~} in the same way that
1140@code{expand-file-name} does.
1141
1142If the target of a symbolic links has remote file name syntax,
1143@code{file-truename} returns it quoted.  @xref{File Name Expansion,,
1144Functions that Expand Filenames}.
1145@end defun
1146
1147@defun file-chase-links filename &optional limit
1148This function follows symbolic links, starting with @var{filename},
1149until it finds a file name which is not the name of a symbolic link.
1150Then it returns that file name.  This function does @emph{not} follow
1151symbolic links at the level of parent directories.
1152
1153If you specify a number for @var{limit}, then after chasing through
1154that many links, the function just returns what it has even if that is
1155still a symbolic link.
1156@end defun
1157
1158  To illustrate the difference between @code{file-chase-links} and
1159@code{file-truename}, suppose that @file{/usr/foo} is a symbolic link to
1160the directory @file{/home/foo}, and @file{/home/foo/hello} is an
1161ordinary file (or at least, not a symbolic link) or nonexistent.  Then
1162we would have:
1163
1164@example
1165(file-chase-links "/usr/foo/hello")
1166     ;; @r{This does not follow the links in the parent directories.}
1167     @result{} "/usr/foo/hello"
1168(file-truename "/usr/foo/hello")
1169     ;; @r{Assuming that @file{/home} is not a symbolic link.}
1170     @result{} "/home/foo/hello"
1171@end example
1172
1173@defun file-equal-p file1 file2
1174This function returns @code{t} if the files @var{file1} and
1175@var{file2} name the same file.  This is similar to comparing their
1176truenames, except that remote file names are also handled in an
1177appropriate manner.  If @var{file1} or @var{file2} does not exist, the
1178return value is unspecified.
1179@end defun
1180
1181@defun file-name-case-insensitive-p filename
1182Sometimes file names or their parts need to be compared as strings, in
1183which case it's important to know whether the underlying filesystem is
1184case-insensitive.  This function returns @code{t} if file
1185@var{filename} is on a case-insensitive filesystem.  It always returns
1186@code{t} on MS-DOS and MS-Windows.  On Cygwin and macOS,
1187filesystems may or may not be case-insensitive, and the function tries
1188to determine case-sensitivity by a runtime test.  If the test is
1189inconclusive, the function returns @code{t} on Cygwin and @code{nil}
1190on macOS.
1191
1192Currently this function always returns @code{nil} on platforms other
1193than MS-DOS, MS-Windows, Cygwin, and macOS@.  It does not detect
1194case-insensitivity of mounted filesystems, such as Samba shares or
1195NFS-mounted Windows volumes.  On remote hosts, it assumes @code{t} for
1196the @samp{smb} method.  For all other connection methods, runtime
1197tests are performed.
1198@end defun
1199
1200@defun file-in-directory-p file dir
1201This function returns @code{t} if @var{file} is a file in directory
1202@var{dir}, or in a subdirectory of @var{dir}.  It also returns
1203@code{t} if @var{file} and @var{dir} are the same directory.  It
1204compares the truenames of the two directories.  If @var{dir} does not
1205name an existing directory, the return value is @code{nil}.
1206@end defun
1207
1208@defun vc-responsible-backend file
1209This function determines the responsible VC backend of the given
1210@var{file}.  For example, if @file{emacs.c} is a file tracked by Git,
1211@w{@code{(vc-responsible-backend "emacs.c")}} returns @samp{Git}.
1212Note that if @var{file} is a symbolic link,
1213@code{vc-responsible-backend} will not resolve it---the backend of the
1214symbolic link file itself is reported.  To get the backend VC of the
1215file to which @var{file} refers, wrap @var{file} with a symbolic link
1216resolving function such as @code{file-chase-links}:
1217
1218@smallexample
1219(vc-responsible-backend (file-chase-links "emacs.c"))
1220@end smallexample
1221@end defun
1222
1223@node File Attributes
1224@subsection File Attributes
1225@cindex file attributes
1226
1227  This section describes the functions for getting detailed
1228information about a file, including the owner and group numbers, the
1229number of names, the inode number, the size, and the times of access
1230and modification.
1231
1232@defun file-newer-than-file-p filename1 filename2
1233@cindex file age
1234@cindex file modification time
1235This function returns @code{t} if the file @var{filename1} is
1236newer than file @var{filename2}.  If @var{filename1} does not
1237exist, it returns @code{nil}.  If @var{filename1} does exist, but
1238@var{filename2} does not, it returns @code{t}.
1239
1240In the following example, assume that the file @file{aug-19} was written
1241on the 19th, @file{aug-20} was written on the 20th, and the file
1242@file{no-file} doesn't exist at all.
1243
1244@example
1245@group
1246(file-newer-than-file-p "aug-19" "aug-20")
1247     @result{} nil
1248@end group
1249@group
1250(file-newer-than-file-p "aug-20" "aug-19")
1251     @result{} t
1252@end group
1253@group
1254(file-newer-than-file-p "aug-19" "no-file")
1255     @result{} t
1256@end group
1257@group
1258(file-newer-than-file-p "no-file" "aug-19")
1259     @result{} nil
1260@end group
1261@end example
1262@end defun
1263
1264@defun file-attributes filename &optional id-format
1265@anchor{Definition of file-attributes}
1266This function returns a list of attributes of file @var{filename}.  If
1267the specified file does not exist, it returns @code{nil}.
1268This function does not follow symbolic links.
1269The optional parameter @var{id-format} specifies the preferred format
1270of attributes @acronym{UID} and @acronym{GID} (see below)---the
1271valid values are @code{'string} and @code{'integer}.  The latter is
1272the default, but we plan to change that, so you should specify a
1273non-@code{nil} value for @var{id-format} if you use the returned
1274@acronym{UID} or @acronym{GID}.
1275
1276On GNU platforms when operating on a local file, this function is
1277atomic: if the filesystem is simultaneously being changed by some
1278other process, this function returns the file's attributes either
1279before or after the change.  Otherwise this function is not atomic,
1280and might return @code{nil} if it detects the race condition, or might
1281return a hodgepodge of the previous and current file attributes.
1282
1283Accessor functions are provided to access the elements in this list.
1284The accessors are mentioned along with the descriptions of the
1285elements below.
1286
1287The elements of the list, in order, are:
1288
1289@enumerate 0
1290@item
1291@code{t} for a directory, a string for a symbolic link (the name
1292linked to), or @code{nil} for a text file
1293(@code{file-attribute-type}).
1294
1295@c Wordy so as to prevent an overfull hbox.  --rjc 15mar92
1296@item
1297The number of names the file has (@code{file-attribute-link-number}).
1298Alternate names, also known as hard links, can be created by using the
1299@code{add-name-to-file} function (@pxref{Changing Files}).
1300
1301@item
1302The file's @acronym{UID}, normally as a string
1303(@code{file-attribute-user-id}).  However, if it does not correspond
1304to a named user, the value is an integer.
1305
1306@item
1307The file's @acronym{GID}, likewise (@code{file-attribute-group-id}).
1308
1309@item
1310The time of last access as a Lisp timestamp
1311(@code{file-attribute-access-time}).  The timestamp is in the
1312style of @code{current-time} (@pxref{Time of Day}) and is truncated
1313to that of the filesystem's timestamp resolution; for example, on some
1314FAT-based filesystems, only the date of last access is recorded, so
1315this time will always hold the midnight of the day of the last access.
1316
1317@cindex modification time of file
1318@item
1319The time of last modification as a Lisp timestamp
1320(@code{file-attribute-modification-time}).  This is the last time when
1321the file's contents were modified.
1322
1323@item
1324The time of last status change as a Lisp timestamp
1325(@code{file-attribute-status-change-time}).  This is the time of the
1326last change to the file's access mode bits, its owner and group, and
1327other information recorded in the filesystem for the file, beyond the
1328file's contents.
1329
1330@item
1331The size of the file in bytes (@code{file-attribute-size}).
1332
1333@item
1334The file's modes, as a string of ten letters or dashes, as in
1335@samp{ls -l} (@code{file-attribute-modes}).
1336
1337@item
1338An unspecified value, present for backward compatibility.
1339
1340@item
1341The file's inode number (@code{file-attribute-inode-number}),
1342a nonnegative integer.
1343
1344@item
1345The filesystem number of the device that the file is on
1346@code{file-attribute-device-number}), an integer.
1347This element and the file's inode number
1348together give enough information to distinguish any two files on the
1349system---no two files can have the same values for both of these
1350numbers.
1351@end enumerate
1352
1353For example, here are the file attributes for @file{files.texi}:
1354
1355@example
1356@group
1357(file-attributes "files.texi" 'string)
1358     @result{}  (nil 1 "lh" "users"
1359          (20614 64019 50040 152000)
1360          (20000 23 0 0)
1361          (20614 64555 902289 872000)
1362          122295 "-rw-rw-rw-"
1363          t 6473924464520138
1364          1014478468)
1365@end group
1366@end example
1367
1368@noindent
1369and here is how the result is interpreted:
1370
1371@table @code
1372@item nil
1373is neither a directory nor a symbolic link.
1374
1375@item 1
1376has only one name (the name @file{files.texi} in the current default
1377directory).
1378
1379@item "lh"
1380is owned by the user with name @samp{lh}.
1381
1382@item "users"
1383is in the group with name @samp{users}.
1384
1385@item (20614 64019 50040 152000)
1386was last accessed on October 23, 2012, at 20:12:03.050040152 UTC.
1387
1388@item (20000 23 0 0)
1389was last modified on July 15, 2001, at 08:53:43 UTC.
1390
1391@item (20614 64555 902289 872000)
1392last had its status changed on October 23, 2012, at 20:20:59.902289872 UTC.
1393
1394@item 122295
1395is 122295 bytes long.  (It may not contain 122295 characters, though,
1396if some of the bytes belong to multibyte sequences, and also if the
1397end-of-line format is CR-LF.)
1398
1399@item "-rw-rw-rw-"
1400has a mode of read and write access for the owner, group, and world.
1401
1402@item t
1403is merely a placeholder; it carries no information.
1404
1405@item 6473924464520138
1406has an inode number of 6473924464520138.
1407
1408@item 1014478468
1409is on the file-system device whose number is 1014478468.
1410@end table
1411@end defun
1412
1413@defun file-nlinks filename
1414This function returns the number of names (i.e., hard links) that
1415file @var{filename} has.  If the file does not exist, this function
1416returns @code{nil}.  Note that symbolic links have no effect on this
1417function, because they are not considered to be names of the files
1418they link to.  This function does not follow symbolic links.
1419
1420@example
1421@group
1422$ ls -l foo*
1423-rw-rw-rw- 2 rms rms 4 Aug 19 01:27 foo
1424-rw-rw-rw- 2 rms rms 4 Aug 19 01:27 foo1
1425@end group
1426
1427@group
1428(file-nlinks "foo")
1429     @result{} 2
1430@end group
1431@group
1432(file-nlinks "doesnt-exist")
1433     @result{} nil
1434@end group
1435@end example
1436@end defun
1437
1438@node Extended Attributes
1439@subsection Extended File Attributes
1440@cindex extended file attributes
1441
1442On some operating systems, each file can be associated with arbitrary
1443@dfn{extended file attributes}.  At present, Emacs supports querying
1444and setting two specific sets of extended file attributes: Access
1445Control Lists (ACLs) and SELinux contexts.  These extended file
1446attributes are used, on some systems, to impose more sophisticated
1447file access controls than the basic Unix-style permissions
1448discussed in the previous sections.
1449
1450@cindex access control list
1451@cindex ACL entries
1452@cindex SELinux context
1453  A detailed explanation of ACLs and SELinux is beyond the scope of
1454this manual.  For our purposes, each file can be associated with an
1455@dfn{ACL}, which specifies its properties under an ACL-based file
1456control system, and/or an @dfn{SELinux context}, which specifies its
1457properties under the SELinux system.
1458
1459@defun file-acl filename
1460This function returns the ACL for the file @var{filename}.  The exact
1461Lisp representation of the ACL is unspecified (and may change in
1462future Emacs versions), but it is the same as what @code{set-file-acl}
1463takes for its @var{acl} argument (@pxref{Changing Files}).
1464
1465The underlying ACL implementation is platform-specific; on GNU/Linux
1466and BSD, Emacs uses the POSIX ACL interface, while on MS-Windows Emacs
1467emulates the POSIX ACL interface with native file security APIs.
1468
1469If ACLs are not supported or the file does not exist,
1470then the return value is @code{nil}.
1471@end defun
1472
1473@defun file-selinux-context filename
1474This function returns the SELinux context of the file @var{filename},
1475as a list of the form @code{(@var{user} @var{role} @var{type}
1476@var{range})}.  The list elements are the context's user, role, type,
1477and range respectively, as Lisp strings; see the SELinux documentation
1478for details about what these actually mean.  The return value has the
1479same form as what @code{set-file-selinux-context} takes for its
1480@var{context} argument (@pxref{Changing Files}).
1481
1482If SELinux is not supported or the file does not exist,
1483then the return value is @code{(nil nil nil nil)}.
1484@end defun
1485
1486@defun file-extended-attributes filename
1487This function returns an alist of the Emacs-recognized extended
1488attributes of file @var{filename}.  Currently, it serves as a
1489convenient way to retrieve both the ACL and SELinux context; you can
1490then call the function @code{set-file-extended-attributes}, with the
1491returned alist as its second argument, to apply the same file access
1492attributes to another file (@pxref{Changing Files}).
1493
1494One of the elements is @code{(acl . @var{acl})}, where @var{acl} has
1495the same form returned by @code{file-acl}.
1496
1497Another element is @code{(selinux-context . @var{context})}, where
1498@var{context} is the SELinux context, in the same form returned by
1499@code{file-selinux-context}.
1500@end defun
1501
1502@node Locating Files
1503@subsection Locating Files in Standard Places
1504@cindex locate file in path
1505@cindex find file in path
1506
1507  This section explains how to search for a file in a list of
1508directories (a @dfn{path}), or for an executable file in the standard
1509list of executable file directories.
1510
1511  To search for a user-specific configuration file, @xref{Standard
1512File Names}, for the @code{locate-user-emacs-file} function.
1513
1514@defun locate-file filename path &optional suffixes predicate
1515This function searches for a file whose name is @var{filename} in a
1516list of directories given by @var{path}, trying the suffixes in
1517@var{suffixes}.  If it finds such a file, it returns the file's
1518absolute file name (@pxref{Relative File Names}); otherwise it returns
1519@code{nil}.
1520
1521The optional argument @var{suffixes} gives the list of file-name
1522suffixes to append to @var{filename} when searching.
1523@code{locate-file} tries each possible directory with each of these
1524suffixes.  If @var{suffixes} is @code{nil}, or @code{("")}, then there
1525are no suffixes, and @var{filename} is used only as-is.  Typical
1526values of @var{suffixes} are @code{exec-suffixes} (@pxref{Subprocess
1527Creation}), @code{load-suffixes}, @code{load-file-rep-suffixes} and
1528the return value of the function @code{get-load-suffixes} (@pxref{Load
1529Suffixes}).
1530
1531Typical values for @var{path} are @code{exec-path} (@pxref{Subprocess
1532Creation}) when looking for executable programs, or @code{load-path}
1533(@pxref{Library Search}) when looking for Lisp files.  If
1534@var{filename} is absolute, @var{path} has no effect, but the suffixes
1535in @var{suffixes} are still tried.
1536
1537The optional argument @var{predicate}, if non-@code{nil}, specifies a
1538predicate function for testing whether a candidate file is suitable.
1539The predicate is passed the candidate file name as its single
1540argument.  If @var{predicate} is @code{nil} or omitted,
1541@code{locate-file} uses @code{file-readable-p} as the predicate.
1542@xref{Kinds of Files}, for other useful predicates, e.g.,
1543@code{file-executable-p} and @code{file-directory-p}.
1544
1545This function will normally skip directories, so if you want it to
1546find directories, make sure the @var{predicate} function returns
1547@code{dir-ok} for them.  For example:
1548
1549@example
1550(locate-file "html" '("/var/www" "/srv") nil
1551             (lambda (f) (if (file-directory-p f) 'dir-ok)))
1552@end example
1553
1554
1555For compatibility, @var{predicate} can also be one of the symbols
1556@code{executable}, @code{readable}, @code{writable}, @code{exists}, or
1557a list of one or more of these symbols.
1558@end defun
1559
1560@defun executable-find program &optional remote
1561This function searches for the executable file of the named
1562@var{program} and returns the absolute file name of the executable,
1563including its file-name extensions, if any.  It returns @code{nil} if
1564the file is not found.  The function searches in all the directories
1565in @code{exec-path}, and tries all the file-name extensions in
1566@code{exec-suffixes} (@pxref{Subprocess Creation}).
1567
1568If @var{remote} is non-@code{nil}, and @code{default-directory} is a
1569remote directory, @var{program} is searched on the respective remote host.
1570@end defun
1571
1572@node Changing Files
1573@section Changing File Names and Attributes
1574@c @cindex renaming files  Duplicates rename-file
1575@cindex copying files
1576@cindex deleting files
1577@cindex linking files
1578@cindex setting modes of files
1579
1580  The functions in this section rename, copy, delete, link, and set
1581the modes (permissions) of files.  Typically, they signal a
1582@code{file-error} error if they fail to perform their function,
1583reporting the system-dependent error message that describes the reason
1584for the failure.  If they fail because a file is missing, they signal
1585a @code{file-missing} error instead.
1586
1587  For performance, the operating system may cache or alias changes
1588made by these functions instead of writing them immediately to
1589secondary storage.  @xref{Files and Storage}.
1590
1591  In the functions that have an argument @var{newname}, if this
1592argument is a directory name it is treated as if the nondirectory part
1593of the source name were appended.  Typically, a directory name is one
1594that ends in @samp{/} (@pxref{Directory Names}).  For example, if the
1595old name is @file{a/b/c}, the @var{newname} @file{d/e/f/} is treated
1596as if it were @file{d/e/f/c}.  This special treatment does not apply
1597if @var{newname} is not a directory name but names a file that is a
1598directory; for example, the @var{newname} @file{d/e/f} is left as-is
1599even if @file{d/e/f} happens to be a directory.
1600
1601  In the functions that have an argument @var{newname}, if a file by the
1602name of @var{newname} already exists, the actions taken depend on the
1603value of the argument @var{ok-if-already-exists}:
1604
1605@itemize @bullet
1606@item
1607Signal a @code{file-already-exists} error if
1608@var{ok-if-already-exists} is @code{nil}.
1609
1610@item
1611Request confirmation if @var{ok-if-already-exists} is a number.
1612
1613@item
1614Replace the old file without confirmation if @var{ok-if-already-exists}
1615is any other value.
1616@end itemize
1617
1618@deffn Command add-name-to-file oldname newname &optional ok-if-already-exists
1619@cindex file with multiple names
1620@cindex file hard link
1621This function gives the file named @var{oldname} the additional name
1622@var{newname}.  This means that @var{newname} becomes a new hard
1623link to @var{oldname}.
1624
1625If @var{newname} is a symbolic link, its directory entry is replaced,
1626not the directory entry it points to.  If @var{oldname} is a symbolic
1627link, this function might or might not follow the link; it does not
1628follow the link on GNU platforms.  If @var{oldname} is a directory,
1629this function typically fails, although for the superuser on a few
1630old-fashioned non-GNU platforms it can succeed and create a filesystem
1631that is not tree-structured.
1632
1633In the first part of the following example, we list two files,
1634@file{foo} and @file{foo3}.
1635
1636@example
1637@group
1638$ ls -li fo*
163981908 -rw-rw-rw- 1 rms rms 29 Aug 18 20:32 foo
164084302 -rw-rw-rw- 1 rms rms 24 Aug 18 20:31 foo3
1641@end group
1642@end example
1643
1644Now we create a hard link, by calling @code{add-name-to-file}, then list
1645the files again.  This shows two names for one file, @file{foo} and
1646@file{foo2}.
1647
1648@example
1649@group
1650(add-name-to-file "foo" "foo2")
1651     @result{} nil
1652@end group
1653
1654@group
1655$ ls -li fo*
165681908 -rw-rw-rw- 2 rms rms 29 Aug 18 20:32 foo
165781908 -rw-rw-rw- 2 rms rms 29 Aug 18 20:32 foo2
165884302 -rw-rw-rw- 1 rms rms 24 Aug 18 20:31 foo3
1659@end group
1660@end example
1661
1662Finally, we evaluate the following:
1663
1664@example
1665(add-name-to-file "foo" "foo3" t)
1666@end example
1667
1668@noindent
1669and list the files again.  Now there are three names
1670for one file: @file{foo}, @file{foo2}, and @file{foo3}.  The old
1671contents of @file{foo3} are lost.
1672
1673@example
1674@group
1675(add-name-to-file "foo1" "foo3")
1676     @result{} nil
1677@end group
1678
1679@group
1680$ ls -li fo*
168181908 -rw-rw-rw- 3 rms rms 29 Aug 18 20:32 foo
168281908 -rw-rw-rw- 3 rms rms 29 Aug 18 20:32 foo2
168381908 -rw-rw-rw- 3 rms rms 29 Aug 18 20:32 foo3
1684@end group
1685@end example
1686
1687This function is meaningless on operating systems where multiple names
1688for one file are not allowed.  Some systems implement multiple names
1689by copying the file instead.
1690
1691See also @code{file-nlinks} in @ref{File Attributes}.
1692@end deffn
1693
1694@deffn Command rename-file filename newname &optional ok-if-already-exists
1695This command renames the file @var{filename} as @var{newname}.
1696
1697If @var{filename} has additional names aside from @var{filename}, it
1698continues to have those names.  In fact, adding the name @var{newname}
1699with @code{add-name-to-file} and then deleting @var{filename} has the
1700same effect as renaming, aside from momentary intermediate states and
1701treatment of errors, directories and symbolic links.
1702
1703This command does not follow symbolic links.  If @var{filename} is a
1704symbolic link, this command renames the symbolic link, not the file it
1705points to.  If @var{newname} is a symbolic link, its directory entry
1706is replaced, not the directory entry it points to.
1707
1708This command does nothing if @var{filename} and @var{newname} are the
1709same directory entry, i.e., if they refer to the same parent directory
1710and give the same name within that directory.  Otherwise, if
1711@var{filename} and @var{newname} name the same file, this command does
1712nothing on POSIX-conforming systems, and removes @var{filename} on
1713some non-POSIX systems.
1714
1715If @var{newname} exists, then it must be an empty directory if
1716@var{oldname} is a directory and a non-directory otherwise.
1717@end deffn
1718
1719@deffn Command copy-file oldname newname &optional ok-if-already-exists time preserve-uid-gid preserve-extended-attributes
1720This command copies the file @var{oldname} to @var{newname}.  An
1721error is signaled if @var{oldname} is not a regular file.  If @var{newname}
1722names a directory, it copies @var{oldname} into that directory,
1723preserving its final name component.
1724@c FIXME: See Bug#27986 for how the previous sentence might change.
1725
1726This function follows symbolic links, except that it does not follow a
1727dangling symbolic link to create @var{newname}.
1728
1729If @var{time} is non-@code{nil}, then this function gives the new file
1730the same last-modified time that the old one has.  (This works on only
1731some operating systems.)  If setting the time gets an error,
1732@code{copy-file} signals a @code{file-date-error} error.  In an
1733interactive call, a prefix argument specifies a non-@code{nil} value
1734for @var{time}.
1735
1736If argument @var{preserve-uid-gid} is @code{nil}, we let the operating
1737system decide the user and group ownership of the new file (this is
1738usually set to the user running Emacs).  If @var{preserve-uid-gid} is
1739non-@code{nil}, we attempt to copy the user and group ownership of the
1740file.  This works only on some operating systems, and only if you have
1741the correct permissions to do so.
1742
1743If the optional argument @var{preserve-permissions} is non-@code{nil},
1744this function copies the file modes (or ``permissions'') of
1745@var{oldname} to @var{newname}, as well as the Access Control List and
1746SELinux context (if any).  @xref{Information about Files}.
1747
1748Otherwise, the file modes of @var{newname} are left unchanged if it is
1749an existing file, and set to those of @var{oldname}, masked by the
1750default file permissions (see @code{set-default-file-modes} below), if
1751@var{newname} is to be newly created.  The Access Control List or
1752SELinux context are not copied over in either case.
1753@end deffn
1754
1755@deffn Command make-symbolic-link target linkname &optional ok-if-already-exists
1756@pindex ln
1757@kindex file-already-exists
1758This command makes a symbolic link to @var{target}, named
1759@var{linkname}.  This is like the shell command @samp{ln -s
1760@var{target} @var{linkname}}.  The @var{target} argument
1761is treated only as a string; it need not name an existing file.
1762If @var{ok-if-already-exists} is an integer, indicating interactive
1763use, then leading @samp{~} is expanded and leading @samp{/:} is
1764stripped in the @var{target} string.
1765
1766If @var{target} is a relative file name, the resulting symbolic link
1767is interpreted relative to the directory containing the symbolic link.
1768@xref{Relative File Names}.
1769
1770If both @var{target} and @var{linkname} have remote file name syntax,
1771and if both remote identifications are equal, the symbolic link points
1772to the local file name part of @var{target}.
1773
1774This function is not available on systems that don't support symbolic
1775links.
1776@end deffn
1777
1778@cindex trash
1779@vindex delete-by-moving-to-trash
1780@deffn Command delete-file filename &optional trash
1781@pindex rm
1782This command deletes the file @var{filename}.  If the file has
1783multiple names, it continues to exist under the other names.  If
1784@var{filename} is a symbolic link, @code{delete-file} deletes only the
1785symbolic link and not its target.
1786
1787A suitable kind of @code{file-error} error is signaled if the file
1788does not exist, or is not deletable.  (On GNU and other POSIX-like
1789systems, a file is deletable if its directory is writable.)
1790
1791If the optional argument @var{trash} is non-@code{nil} and the
1792variable @code{delete-by-moving-to-trash} is non-@code{nil}, this
1793command moves the file into the system Trash instead of deleting it.
1794@xref{Misc File Ops,,Miscellaneous File Operations, emacs, The GNU
1795Emacs Manual}.  When called interactively, @var{trash} is @code{t} if
1796no prefix argument is given, and @code{nil} otherwise.
1797
1798See also @code{delete-directory} in @ref{Create/Delete Dirs}.
1799@end deffn
1800
1801@cindex file permissions, setting
1802@cindex permissions, file
1803@cindex file modes, setting
1804@deffn Command set-file-modes filename mode
1805This function sets the @dfn{file mode} (or @dfn{permissions}) of
1806@var{filename} to @var{mode}.  This function follows symbolic links.
1807
1808If called non-interactively, @var{mode} must be an integer.  Only the
1809lowest 12 bits of the integer are used; on most systems, only the
1810lowest 9 bits are meaningful.  You can use the Lisp construct for
1811octal numbers to enter @var{mode}.  For example,
1812
1813@example
1814(set-file-modes #o644)
1815@end example
1816
1817@noindent
1818specifies that the file should be readable and writable for its owner,
1819readable for group members, and readable for all other users.
1820@xref{File permissions,,, coreutils, The @sc{gnu} @code{Coreutils}
1821Manual}, for a description of mode bit specifications.
1822
1823Interactively, @var{mode} is read from the minibuffer using
1824@code{read-file-modes} (see below), which lets the user type in either
1825an integer or a string representing the permissions symbolically.
1826
1827@xref{File Attributes}, for the function @code{file-modes}, which
1828returns the permissions of a file.
1829@end deffn
1830
1831@defun set-default-file-modes mode
1832@cindex umask
1833This function sets the default permissions for new files created by
1834Emacs and its subprocesses.  Every file created with Emacs initially
1835has these permissions, or a subset of them (@code{write-region} will
1836not grant execute permissions even if the default file permissions
1837allow execution).  On GNU and other POSIX-like systems, the default
1838permissions are given by the bitwise complement of the @samp{umask}
1839value, i.e.@: each bit that is set in the argument @var{mode} will be
1840@emph{reset} in the default permissions with which Emacs creates
1841files.
1842
1843The argument @var{mode} should be an integer which specifies the
1844permissions, similar to @code{set-file-modes} above.  Only the lowest
18459 bits are meaningful.
1846
1847The default file permissions have no effect when you save a modified
1848version of an existing file; saving a file preserves its existing
1849permissions.
1850@end defun
1851
1852@defmac with-file-modes mode body@dots{}
1853This macro evaluates the @var{body} forms with the default
1854permissions for new files temporarily set to @var{modes} (whose value
1855is as for @code{set-file-modes} above).  When finished, it restores
1856the original default file permissions, and returns the value of the
1857last form in @var{body}.
1858
1859This is useful for creating private files, for example.
1860@end defmac
1861
1862@defun default-file-modes
1863This function returns the default file permissions, as an integer.
1864@end defun
1865
1866@defun read-file-modes &optional prompt base-file
1867This function reads a set of file mode bits from the minibuffer.  The
1868first optional argument @var{prompt} specifies a non-default prompt.
1869Second second optional argument @var{base-file} is the name of a file
1870on whose permissions to base the mode bits that this function returns,
1871if what the user types specifies mode bits relative to permissions of
1872an existing file.
1873
1874If user input represents an octal number, this function returns that
1875number.  If it is a complete symbolic specification of mode bits, as
1876in @code{"u=rwx"}, the function converts it to the equivalent numeric
1877value using @code{file-modes-symbolic-to-number} and returns the
1878result.  If the specification is relative, as in @code{"o+g"}, then
1879the permissions on which the specification is based are taken from the
1880mode bits of @var{base-file}.  If @var{base-file} is omitted or
1881@code{nil}, the function uses @code{0} as the base mode bits.  The
1882complete and relative specifications can be combined, as in
1883@code{"u+r,g+rx,o+r,g-w"}.  @xref{File permissions,,, coreutils, The
1884@sc{gnu} @code{Coreutils} Manual}, for a description of file mode
1885specifications.
1886@end defun
1887
1888@defun file-modes-symbolic-to-number modes &optional base-modes
1889This function converts a symbolic file mode specification in
1890@var{modes} into the equivalent integer.  If the symbolic
1891specification is based on an existing file, that file's mode bits are
1892taken from the optional argument @var{base-modes}; if that argument is
1893omitted or @code{nil}, it defaults to 0, i.e., no access rights at
1894all.
1895@end defun
1896
1897@defun set-file-times filename &optional time
1898This function sets the access and modification times of @var{filename}
1899to @var{time}.  The return value is @code{t} if the times are successfully
1900set, otherwise it is @code{nil}.  @var{time} defaults to the current
1901time and must be a time value (@pxref{Time of Day}).
1902@end defun
1903
1904@defun set-file-extended-attributes filename attribute-alist
1905This function sets the Emacs-recognized extended file attributes for
1906@code{filename}.  The second argument @var{attribute-alist} should be
1907an alist of the same form returned by @code{file-extended-attributes}.
1908The return value is @code{t} if the attributes are successfully set,
1909otherwise it is @code{nil}.
1910@xref{Extended Attributes}.
1911@end defun
1912
1913@defun set-file-selinux-context filename context
1914This function sets the SELinux security context for @var{filename} to
1915@var{context}.  The @var{context} argument should be a list
1916@code{(@var{user} @var{role} @var{type} @var{range})}, where each
1917element is a string.  @xref{Extended Attributes}.
1918
1919The function returns @code{t} if it succeeds in setting the SELinux
1920context of @var{filename}.  It returns @code{nil} if the context was
1921not set (e.g., if SELinux is disabled, or if Emacs was compiled
1922without SELinux support).
1923@end defun
1924
1925@defun set-file-acl filename acl
1926This function sets the Access Control List for @var{filename} to
1927@var{acl}.  The @var{acl} argument should have the same form returned
1928by the function @code{file-acl}.  @xref{Extended Attributes}.
1929
1930The function returns @code{t} if it successfully sets the ACL of
1931@var{filename}, @code{nil} otherwise.
1932@end defun
1933
1934@node Files and Storage
1935@section Files and Secondary Storage
1936@cindex secondary storage
1937
1938After Emacs changes a file, there are two reasons the changes might
1939not survive later failures of power or media, both having to do with
1940efficiency.  First, the operating system might alias written data with
1941data already stored elsewhere on secondary storage until one file or
1942the other is later modified; this will lose both files if the only
1943copy on secondary storage is lost due to media failure.  Second, the
1944operating system might not write data to secondary storage
1945immediately, which will lose the data if power is lost.
1946
1947@findex write-region
1948Although both sorts of failures can largely be avoided by a suitably
1949configured file system, such systems are typically more expensive or
1950less efficient.  In more-typical systems, to survive media failure you
1951can copy the file to a different device, and to survive a power
1952failure you can use the @code{write-region} function with the
1953@code{write-region-inhibit-fsync} variable set to @code{nil}.
1954@xref{Writing to Files}.
1955
1956@node File Names
1957@section File Names
1958@cindex file names
1959
1960  Files are generally referred to by their names, in Emacs as elsewhere.
1961File names in Emacs are represented as strings.  The functions that
1962operate on a file all expect a file name argument.
1963
1964  In addition to operating on files themselves, Emacs Lisp programs
1965often need to operate on file names; i.e., to take them apart and to use
1966part of a name to construct related file names.  This section describes
1967how to manipulate file names.
1968
1969  The functions in this section do not actually access files, so they
1970can operate on file names that do not refer to an existing file or
1971directory.
1972
1973@findex cygwin-convert-file-name-from-windows
1974@findex cygwin-convert-file-name-to-windows
1975@cindex MS-Windows file-name syntax
1976@cindex converting file names from/to MS-Windows syntax
1977  On MS-DOS and MS-Windows, these functions (like the function that
1978actually operate on files) accept MS-DOS or MS-Windows file-name syntax,
1979where backslashes separate the components, as well as POSIX syntax; but
1980they always return POSIX syntax.  This enables Lisp programs to specify
1981file names in POSIX syntax and work properly on all systems without
1982change.@footnote{In MS-Windows versions of Emacs compiled for the Cygwin
1983environment, you can use the functions
1984@code{cygwin-convert-file-name-to-windows} and
1985@code{cygwin-convert-file-name-from-windows} to convert between the
1986two file-name syntaxes.}
1987
1988@menu
1989* File Name Components::  The directory part of a file name, and the rest.
1990* Relative File Names::   Some file names are relative to a current directory.
1991* Directory Names::       A directory's name as a directory
1992                            is different from its name as a file.
1993* File Name Expansion::   Converting relative file names to absolute ones.
1994* Unique File Names::     Generating names for temporary files.
1995* File Name Completion::  Finding the completions for a given file name.
1996* Standard File Names::   If your package uses a fixed file name,
1997                            how to handle various operating systems simply.
1998@end menu
1999
2000@node File Name Components
2001@subsection File Name Components
2002@cindex directory part (of file name)
2003@cindex nondirectory part (of file name)
2004@cindex version number (in file name)
2005
2006  The operating system groups files into directories.  To specify a
2007file, you must specify the directory and the file's name within that
2008directory.  Therefore, Emacs considers a file name as having two main
2009parts: the @dfn{directory name} part, and the @dfn{nondirectory} part
2010(or @dfn{file name within the directory}).  Either part may be empty.
2011Concatenating these two parts reproduces the original file name.
2012
2013  On most systems, the directory part is everything up to and including
2014the last slash (backslash is also allowed in input on MS-DOS or
2015MS-Windows); the nondirectory part is the rest.
2016
2017  For some purposes, the nondirectory part is further subdivided into
2018the name proper and the @dfn{version number}.  On most systems, only
2019backup files have version numbers in their names.
2020
2021@defun file-name-directory filename
2022This function returns the directory part of @var{filename}, as a
2023directory name (@pxref{Directory Names}), or @code{nil} if
2024@var{filename} does not include a directory part.
2025
2026On GNU and other POSIX-like systems, a string returned by this function always
2027ends in a slash.  On MS-DOS it can also end in a colon.
2028
2029@example
2030@group
2031(file-name-directory "lewis/foo")  ; @r{GNU example}
2032     @result{} "lewis/"
2033@end group
2034@group
2035(file-name-directory "foo")        ; @r{GNU example}
2036     @result{} nil
2037@end group
2038@end example
2039@end defun
2040
2041@defun file-name-nondirectory filename
2042This function returns the nondirectory part of @var{filename}.
2043
2044@example
2045@group
2046(file-name-nondirectory "lewis/foo")
2047     @result{} "foo"
2048@end group
2049@group
2050(file-name-nondirectory "foo")
2051     @result{} "foo"
2052@end group
2053@group
2054(file-name-nondirectory "lewis/")
2055     @result{} ""
2056@end group
2057@end example
2058@end defun
2059
2060@defun file-name-sans-versions filename &optional keep-backup-version
2061This function returns @var{filename} with any file version numbers,
2062backup version numbers, or trailing tildes discarded.
2063
2064If @var{keep-backup-version} is non-@code{nil}, then true file version
2065numbers understood as such by the file system are discarded from the
2066return value, but backup version numbers are kept.
2067
2068@example
2069@group
2070(file-name-sans-versions "~rms/foo.~1~")
2071     @result{} "~rms/foo"
2072@end group
2073@group
2074(file-name-sans-versions "~rms/foo~")
2075     @result{} "~rms/foo"
2076@end group
2077@group
2078(file-name-sans-versions "~rms/foo")
2079     @result{} "~rms/foo"
2080@end group
2081@end example
2082@end defun
2083
2084@defun file-name-extension filename &optional period
2085This function returns @var{filename}'s final extension, if any,
2086after applying @code{file-name-sans-versions} to remove any
2087version/backup part.  The extension, in a file name, is the part that
2088follows the last @samp{.} in the last name component (minus any
2089version/backup part).
2090
2091This function returns @code{nil} for extensionless file names such as
2092@file{foo}.  It returns @code{""} for null extensions, as in
2093@file{foo.}.  If the last component of a file name begins with a
2094@samp{.}, that @samp{.}  doesn't count as the beginning of an
2095extension.  Thus, @file{.emacs}'s extension is @code{nil}, not
2096@samp{.emacs}.
2097
2098If @var{period} is non-@code{nil}, then the returned value includes
2099the period that delimits the extension, and if @var{filename} has no
2100extension, the value is @code{""}.
2101@end defun
2102
2103@defun file-name-sans-extension filename
2104This function returns @var{filename} minus its extension, if any.  The
2105version/backup part, if present, is only removed if the file has an
2106extension.  For example,
2107
2108@example
2109(file-name-sans-extension "foo.lose.c")
2110     @result{} "foo.lose"
2111(file-name-sans-extension "big.hack/foo")
2112     @result{} "big.hack/foo"
2113(file-name-sans-extension "/my/home/.emacs")
2114     @result{} "/my/home/.emacs"
2115(file-name-sans-extension "/my/home/.emacs.el")
2116     @result{} "/my/home/.emacs"
2117(file-name-sans-extension "~/foo.el.~3~")
2118     @result{} "~/foo"
2119(file-name-sans-extension "~/foo.~3~")
2120     @result{} "~/foo.~3~"
2121@end example
2122
2123Note that the @samp{.~3~} in the two last examples is the backup part,
2124not an extension.
2125@end defun
2126
2127@defun file-name-base filename
2128This function is the composition of @code{file-name-sans-extension}
2129and @code{file-name-nondirectory}.  For example,
2130
2131@example
2132(file-name-base "/my/home/foo.c")
2133    @result{} "foo"
2134@end example
2135@end defun
2136
2137@node Relative File Names
2138@subsection Absolute and Relative File Names
2139@cindex absolute file name
2140@cindex relative file name
2141
2142  All the directories in the file system form a tree starting at the
2143root directory.  A file name can specify all the directory names
2144starting from the root of the tree; then it is called an
2145@dfn{absolute} file name.  Or it can specify the position of the file
2146in the tree relative to a default directory; then it is called a
2147@dfn{relative} file name.  On GNU and other POSIX-like systems,
2148after any leading @samp{~} has been expanded, an absolute file name
2149starts with a @samp{/}
2150(@pxref{abbreviate-file-name}), and a relative one does not.  On
2151MS-DOS and MS-Windows, an absolute file name starts with a slash or a
2152backslash, or with a drive specification @samp{@var{x}:/}, where
2153@var{x} is the @dfn{drive letter}.
2154
2155@defun file-name-absolute-p filename
2156This function returns @code{t} if file @var{filename} is an absolute
2157file name, @code{nil} otherwise.  A file name is considered to be
2158absolute if its first component is @samp{~}, or is @samp{~@var{user}}
2159where @var{user} is a valid login name.  In the following examples,
2160assume that there is a user named @samp{rms} but no user named
2161@samp{nosuchuser}.
2162
2163@example
2164@group
2165(file-name-absolute-p "~rms/foo")
2166     @result{} t
2167@end group
2168@group
2169(file-name-absolute-p "~nosuchuser/foo")
2170     @result{} nil
2171@end group
2172@group
2173(file-name-absolute-p "rms/foo")
2174     @result{} nil
2175@end group
2176@group
2177(file-name-absolute-p "/user/rms/foo")
2178     @result{} t
2179@end group
2180@end example
2181@end defun
2182
2183  Given a possibly relative file name, you can expand any
2184leading @samp{~} and convert the result to an
2185absolute name using @code{expand-file-name} (@pxref{File Name
2186Expansion}).  This function converts absolute file names to relative
2187names:
2188
2189@defun file-relative-name filename &optional directory
2190This function tries to return a relative name that is equivalent to
2191@var{filename}, assuming the result will be interpreted relative to
2192@var{directory} (an absolute directory name or directory file name).
2193If @var{directory} is omitted or @code{nil}, it defaults to the
2194current buffer's default directory.
2195
2196On some operating systems, an absolute file name begins with a device
2197name.  On such systems, @var{filename} has no relative equivalent based
2198on @var{directory} if they start with two different device names.  In
2199this case, @code{file-relative-name} returns @var{filename} in absolute
2200form.
2201
2202@example
2203(file-relative-name "/foo/bar" "/foo/")
2204     @result{} "bar"
2205(file-relative-name "/foo/bar" "/hack/")
2206     @result{} "../foo/bar"
2207@end example
2208@end defun
2209
2210@node Directory Names
2211@subsection Directory Names
2212@cindex directory name
2213@cindex directory file name
2214@cindex file name of directory
2215
2216  A @dfn{directory name} is a string that must name a directory if it
2217names any file at all.  A directory is actually a kind of file, and it
2218has a file name (called the @dfn{directory file name}, which is
2219related to the directory name but is typically not identical.  (This
2220is not quite the same as the usual POSIX terminology.)  These two
2221names for the same entity are related by a syntactic transformation.
2222On GNU and other POSIX-like systems, this is simple: to obtain a
2223directory name, append a @samp{/} to a directory file name that does
2224not already end in @samp{/}.  On MS-DOS the relationship is more
2225complicated.
2226
2227  The difference between a directory name and a directory file name is
2228subtle but crucial.  When an Emacs variable or function argument is
2229described as being a directory name, a directory file name is not
2230acceptable.  When @code{file-name-directory} returns a string, that is
2231always a directory name.
2232
2233  The following two functions convert between directory names and
2234directory file names.  They do nothing special with environment
2235variable substitutions such as @samp{$HOME}, and the constructs
2236@samp{~}, @samp{.} and @samp{..}.
2237
2238@defun file-name-as-directory filename
2239This function returns a string representing @var{filename} in a form
2240that the operating system will interpret as the name of a directory (a
2241directory name).  On most systems, this means appending a slash to the
2242string (if it does not already end in one).
2243
2244@example
2245@group
2246(file-name-as-directory "~rms/lewis")
2247     @result{} "~rms/lewis/"
2248@end group
2249@end example
2250@end defun
2251
2252@defun directory-name-p filename
2253This function returns non-@code{nil} if @var{filename} ends with a
2254directory separator character.  This is the forward slash @samp{/} on
2255GNU and other POSIX-like systems; MS-Windows and MS-DOS recognize both
2256the forward slash and the backslash @samp{\} as directory separators.
2257@end defun
2258
2259@defun directory-file-name dirname
2260This function returns a string representing @var{dirname} in a form
2261that the operating system will interpret as the name of a file (a
2262directory file name).  On most systems, this means removing the final
2263directory separators from the string, unless the string consists
2264entirely of directory separators.
2265
2266@example
2267@group
2268(directory-file-name "~lewis/")
2269     @result{} "~lewis"
2270@end group
2271@end example
2272@end defun
2273
2274  Given a directory name, you can combine it with a relative file name
2275using @code{concat}:
2276
2277@example
2278(concat @var{dirname} @var{relfile})
2279@end example
2280
2281@noindent
2282Be sure to verify that the file name is relative before doing that.
2283If you use an absolute file name, the results could be syntactically
2284invalid or refer to the wrong file.
2285
2286  If you want to use a directory file name in making such a
2287combination, you must first convert it to a directory name using
2288@code{file-name-as-directory}:
2289
2290@example
2291(concat (file-name-as-directory @var{dirfile}) @var{relfile})
2292@end example
2293
2294@noindent
2295Don't try concatenating a slash by hand, as in
2296
2297@example
2298;;; @r{Wrong!}
2299(concat @var{dirfile} "/" @var{relfile})
2300@end example
2301
2302@noindent
2303because this is not portable.  Always use
2304@code{file-name-as-directory}.
2305
2306  To avoid the issues mentioned above, or if the @var{dirname} value
2307might be @code{nil} (for example, from an element of @code{load-path}),
2308use:
2309
2310@example
2311(expand-file-name @var{relfile} @var{dirname})
2312@end example
2313
2314However, @code{expand-file-name} expands leading @samp{~} in
2315@var{relfile}, which may not be what you want.  @xref{File Name
2316Expansion}.
2317
2318  To convert a directory name to its abbreviation, use this
2319function:
2320
2321@cindex file name abbreviations
2322@cindex abbreviated file names
2323@vindex directory-abbrev-alist
2324@defun abbreviate-file-name filename
2325@anchor{abbreviate-file-name}
2326This function returns an abbreviated form of @var{filename}.  It
2327applies the abbreviations specified in @code{directory-abbrev-alist}
2328(@pxref{File Aliases,,File Aliases, emacs, The GNU Emacs Manual}),
2329then substitutes @samp{~} for the user's home directory if the
2330argument names a file in the home directory or one of its
2331subdirectories.  If the home directory is a root directory, it is not
2332replaced with @samp{~}, because this does not make the result shorter
2333on many systems.
2334
2335You can use this function for directory names and for file names,
2336because it recognizes abbreviations even as part of the name.
2337@end defun
2338
2339@node File Name Expansion
2340@subsection Functions that Expand Filenames
2341@cindex expansion of file names
2342
2343  @dfn{Expanding} a file name means converting a relative file name to
2344an absolute one.  Since this is done relative to a default directory,
2345you must specify the default directory as well as the file name
2346to be expanded.  It also involves expanding abbreviations like
2347@file{~/}
2348@ifnottex
2349(@pxref{abbreviate-file-name}),
2350@end ifnottex
2351and eliminating redundancies like @file{./} and @file{@var{name}/../}.
2352
2353@defun expand-file-name filename &optional directory
2354This function converts @var{filename} to an absolute file name.  If
2355@var{directory} is supplied, it is the default directory to start with
2356if @var{filename} is relative and does not start with @samp{~}.
2357(The value of @var{directory} should
2358itself be an absolute directory name or directory file name; it may
2359start with @samp{~}.)  Otherwise, the current buffer's value of
2360@code{default-directory} is used.  For example:
2361
2362@example
2363@group
2364(expand-file-name "foo")
2365     @result{} "/xcssun/users/rms/lewis/foo"
2366@end group
2367@group
2368(expand-file-name "../foo")
2369     @result{} "/xcssun/users/rms/foo"
2370@end group
2371@group
2372(expand-file-name "foo" "/usr/spool/")
2373     @result{} "/usr/spool/foo"
2374@end group
2375@end example
2376
2377If the part of @var{filename} before the first slash is
2378@samp{~}, it expands to your home directory, which is typically
2379specified by the value of the @env{HOME} environment variable
2380(@pxref{General Variables,,, emacs, The GNU Emacs Manual}).
2381If the part before the first
2382slash is @samp{~@var{user}} and if @var{user} is a valid login name,
2383it expands to @var{user}'s home directory.
2384If you do not want this expansion for a relative @var{filename} that
2385might begin with a literal @samp{~}, you can use @code{(concat
2386(file-name-as-directory directory) filename)} instead of
2387@code{(expand-file-name filename directory)}.
2388
2389Filenames containing @samp{.} or @samp{..} are simplified to their
2390canonical form:
2391
2392@example
2393@group
2394(expand-file-name "bar/../foo")
2395     @result{} "/xcssun/users/rms/lewis/foo"
2396@end group
2397@end example
2398
2399In some cases, a leading @samp{..} component can remain in the output:
2400
2401@example
2402@group
2403(expand-file-name "../home" "/")
2404     @result{} "/../home"
2405@end group
2406@end example
2407
2408@noindent
2409This is for the sake of filesystems that have the concept of a
2410superroot above the root directory @file{/}.  On other filesystems,
2411@file{/../} is interpreted exactly the same as @file{/}.
2412
2413Expanding @file{.} or the empty string returns the default directory:
2414
2415@example
2416@group
2417(expand-file-name "." "/usr/spool/")
2418     @result{} "/usr/spool"
2419(expand-file-name "" "/usr/spool/")
2420     @result{} "/usr/spool"
2421@end group
2422@end example
2423
2424Note that @code{expand-file-name} does @emph{not} expand environment
2425variables; only @code{substitute-in-file-name} does that:
2426
2427@example
2428@group
2429(expand-file-name "$HOME/foo")
2430     @result{} "/xcssun/users/rms/lewis/$HOME/foo"
2431@end group
2432@end example
2433
2434Note also that @code{expand-file-name} does not follow symbolic links
2435at any level.  This results in a difference between the way
2436@code{file-truename} and @code{expand-file-name} treat @samp{..}.
2437Assuming that @samp{/tmp/bar} is a symbolic link to the directory
2438@samp{/tmp/foo/bar} we get:
2439
2440@example
2441@group
2442(file-truename "/tmp/bar/../myfile")
2443     @result{} "/tmp/foo/myfile"
2444@end group
2445@group
2446(expand-file-name "/tmp/bar/../myfile")
2447     @result{} "/tmp/myfile"
2448@end group
2449@end example
2450
2451If you may need to follow symbolic links preceding @samp{..}, you
2452should make sure to call @code{file-truename} without prior direct or
2453indirect calls to @code{expand-file-name}.  @xref{Truenames}.
2454@end defun
2455
2456@defvar default-directory
2457The value of this buffer-local variable is the default directory for the
2458current buffer.  It should be an absolute directory name; it may start
2459with @samp{~}.  This variable is buffer-local in every buffer.
2460
2461@code{expand-file-name} uses the default directory when its second
2462argument is @code{nil}.
2463
2464The value is always a string ending with a slash.
2465
2466@example
2467@group
2468default-directory
2469     @result{} "/user/lewis/manual/"
2470@end group
2471@end example
2472@end defvar
2473
2474@defun substitute-in-file-name filename
2475@anchor{Definition of substitute-in-file-name}
2476This function replaces environment variable references in
2477@var{filename} with the environment variable values.  Following
2478standard Unix shell syntax, @samp{$} is the prefix to substitute an
2479environment variable value.  If the input contains @samp{$$}, that is
2480converted to @samp{$}; this gives the user a way to quote a
2481@samp{$}.
2482
2483The environment variable name is the series of alphanumeric characters
2484(including underscores) that follow the @samp{$}.  If the character following
2485the @samp{$} is a @samp{@{}, then the variable name is everything up to the
2486matching @samp{@}}.
2487
2488Calling @code{substitute-in-file-name} on output produced by
2489@code{substitute-in-file-name} tends to give incorrect results.  For
2490instance, use of @samp{$$} to quote a single @samp{$} won't work
2491properly, and @samp{$} in an environment variable's value could lead
2492to repeated substitution.  Therefore, programs that call this function
2493and put the output where it will be passed to this function need to
2494double all @samp{$} characters to prevent subsequent incorrect
2495results.
2496
2497@c Wordy to avoid overfull hbox.  --rjc 15mar92
2498Here we assume that the environment variable @env{HOME}, which holds
2499the user's home directory, has value @samp{/xcssun/users/rms}.
2500
2501@example
2502@group
2503(substitute-in-file-name "$HOME/foo")
2504     @result{} "/xcssun/users/rms/foo"
2505@end group
2506@end example
2507
2508After substitution, if a @samp{~} or a @samp{/} appears immediately
2509after another @samp{/}, the function discards everything before it (up
2510through the immediately preceding @samp{/}).
2511
2512@example
2513@group
2514(substitute-in-file-name "bar/~/foo")
2515     @result{} "~/foo"
2516@end group
2517@group
2518(substitute-in-file-name "/usr/local/$HOME/foo")
2519     @result{} "/xcssun/users/rms/foo"
2520     ;; @r{@file{/usr/local/} has been discarded.}
2521@end group
2522@end example
2523
2524@end defun
2525
2526  Sometimes, it is not desired to expand file names.  In such cases,
2527the file name can be quoted to suppress the expansion, and to handle
2528the file name literally.  Quoting happens by prefixing the file name
2529with @samp{/:}.
2530
2531@defmac file-name-quote name
2532This macro adds the quotation prefix @samp{/:} to the file @var{name}.
2533For a local file @var{name}, it prefixes @var{name} with @samp{/:}.
2534If @var{name} is a remote file name, the local part of @var{name}
2535(@pxref{Magic File Names}) is quoted.  If @var{name} is already a
2536quoted file name, @var{name} is returned unchanged.
2537
2538@example
2539@group
2540(substitute-in-file-name (file-name-quote "bar/~/foo"))
2541     @result{} "/:bar/~/foo"
2542@end group
2543
2544@group
2545(substitute-in-file-name (file-name-quote "/ssh:host:bar/~/foo"))
2546     @result{} "/ssh:host:/:bar/~/foo"
2547@end group
2548@end example
2549
2550The macro cannot be used to suppress file name handlers from magic
2551file names (@pxref{Magic File Names}).
2552@end defmac
2553
2554@defmac file-name-unquote name
2555This macro removes the quotation prefix @samp{/:} from the file
2556@var{name}, if any. If @var{name} is a remote file name, the local
2557part of @var{name} is unquoted.
2558@end defmac
2559
2560@defmac file-name-quoted-p name
2561This macro returns non-@code{nil}, when @var{name} is quoted with the
2562prefix @samp{/:}.  If @var{name} is a remote file name, the local part
2563of @var{name} is checked.
2564@end defmac
2565
2566
2567@node Unique File Names
2568@subsection Generating Unique File Names
2569@cindex unique file names
2570@cindex temporary files
2571
2572  Some programs need to write temporary files.  Here is the usual way to
2573construct a name for such a file:
2574
2575@example
2576(make-temp-file @var{name-of-application})
2577@end example
2578
2579@noindent
2580The job of @code{make-temp-file} is to prevent two different users or
2581two different jobs from trying to use the exact same file name.
2582
2583@defun make-temp-file prefix &optional dir-flag suffix text
2584This function creates a temporary file and returns its name.  Emacs
2585creates the temporary file's name by adding to @var{prefix} some
2586random characters that are different in each Emacs job.  The result is
2587guaranteed to be a newly created file, containing @var{text} if that's
2588given as a string and empty otherwise. On MS-DOS, this function can
2589truncate @var{prefix} to fit into the 8+3 file-name limits.  If
2590@var{prefix} is a relative file name, it is expanded against
2591@code{temporary-file-directory}.
2592
2593@example
2594@group
2595(make-temp-file "foo")
2596     @result{} "/tmp/foo232J6v"
2597@end group
2598@end example
2599
2600When @code{make-temp-file} returns, the file has been created and is
2601empty.  At that point, you should write the intended contents into the
2602file.
2603
2604If @var{dir-flag} is non-@code{nil}, @code{make-temp-file} creates an
2605empty directory instead of an empty file.  It returns the file name,
2606not the directory name, of that directory.  @xref{Directory Names}.
2607
2608If @var{suffix} is non-@code{nil}, @code{make-temp-file} adds it at
2609the end of the file name.
2610
2611If @var{text} is a string, @code{make-temp-file} inserts it in the file.
2612
2613To prevent conflicts among different libraries running in the same
2614Emacs, each Lisp program that uses @code{make-temp-file} should have its
2615own @var{prefix}.  The number added to the end of @var{prefix}
2616distinguishes between the same application running in different Emacs
2617jobs.  Additional added characters permit a large number of distinct
2618names even in one Emacs job.
2619@end defun
2620
2621  The default directory for temporary files is controlled by the
2622variable @code{temporary-file-directory}.  This variable gives the user
2623a uniform way to specify the directory for all temporary files.  Some
2624programs use @code{small-temporary-file-directory} instead, if that is
2625non-@code{nil}.  To use it, you should expand the prefix against
2626the proper directory before calling @code{make-temp-file}.
2627
2628@defopt temporary-file-directory
2629@cindex @env{TMPDIR} environment variable
2630@cindex @env{TMP} environment variable
2631@cindex @env{TEMP} environment variable
2632This variable specifies the directory name for creating temporary files.
2633Its value should be a directory name (@pxref{Directory Names}), but it
2634is good for Lisp programs to cope if the value is a directory's file
2635name instead.  Using the value as the second argument to
2636@code{expand-file-name} is a good way to achieve that.
2637
2638The default value is determined in a reasonable way for your operating
2639system; it is based on the @env{TMPDIR}, @env{TMP} and @env{TEMP}
2640environment variables, with a fall-back to a system-dependent name if
2641none of these variables is defined.
2642
2643Even if you do not use @code{make-temp-file} to create the temporary
2644file, you should still use this variable to decide which directory to
2645put the file in.  However, if you expect the file to be small, you
2646should use @code{small-temporary-file-directory} first if that is
2647non-@code{nil}.
2648@end defopt
2649
2650@defopt small-temporary-file-directory
2651This variable specifies the directory name for
2652creating certain temporary files, which are likely to be small.
2653
2654If you want to write a temporary file which is likely to be small, you
2655should compute the directory like this:
2656
2657@example
2658(make-temp-file
2659  (expand-file-name @var{prefix}
2660                    (or small-temporary-file-directory
2661                        temporary-file-directory)))
2662@end example
2663@end defopt
2664
2665@defun make-temp-name base-name
2666This function generates a string that might be a unique file
2667name.  The name starts with @var{base-name}, and has several random
2668characters appended to it, which are different in each Emacs job.  It
2669is like @code{make-temp-file} except that (i) it just constructs a
2670name and does not create a file, (ii) @var{base-name} should be an
2671absolute file name that is not magic, and (iii) if the returned file
2672name is magic, it might name an existing file.  @xref{Magic File
2673Names}.
2674
2675@strong{Warning:} In most cases, you should not use this function; use
2676@code{make-temp-file} instead!  This function is susceptible to a race
2677condition, between the @code{make-temp-name} call and the creation of
2678the file, which in some cases may cause a security hole.
2679@end defun
2680
2681Sometimes, it is necessary to create a temporary file on a remote host
2682or a mounted directory.  The following two functions support this.
2683
2684@cindex temporary file on a remote host
2685@defun make-nearby-temp-file prefix &optional dir-flag suffix
2686This function is similar to @code{make-temp-file}, but it creates a
2687temporary file as close as possible to @code{default-directory}.  If
2688@var{prefix} is a relative file name, and @code{default-directory} is
2689a remote file name or located on a mounted file systems, the temporary
2690file is created in the directory returned by the function
2691@code{temporary-file-directory}.  Otherwise, the function
2692@code{make-temp-file} is used.  @var{prefix}, @var{dir-flag} and
2693@var{suffix} have the same meaning as in @code{make-temp-file}.
2694
2695@example
2696@group
2697(let ((default-directory "/ssh:remotehost:"))
2698  (make-nearby-temp-file "foo"))
2699     @result{} "/ssh:remotehost:/tmp/foo232J6v"
2700@end group
2701@end example
2702@end defun
2703
2704@defun temporary-file-directory
2705The directory for writing temporary files via
2706@code{make-nearby-temp-file}.  In case of a remote
2707@code{default-directory}, this is a directory for temporary files on
2708that remote host.  If such a directory does not exist, or
2709@code{default-directory} ought to be located on a mounted file system
2710(see @code{mounted-file-systems}), the function returns
2711@code{default-directory}.  For a non-remote and non-mounted
2712@code{default-directory}, the value of the variable
2713@code{temporary-file-directory} is returned.
2714@end defun
2715
2716In order to extract the local part of the file's name of a temporary
2717file, use @code{file-local-name} (@pxref{Magic File Names}).
2718
2719@node File Name Completion
2720@subsection File Name Completion
2721@cindex file name completion subroutines
2722@cindex completion, file name
2723
2724  This section describes low-level subroutines for completing a file
2725name.  For higher level functions, see @ref{Reading File Names}.
2726
2727@defun file-name-all-completions partial-filename directory
2728This function returns a list of all possible completions for a file
2729whose name starts with @var{partial-filename} in directory
2730@var{directory}.  The order of the completions is the order of the files
2731in the directory, which is unpredictable and conveys no useful
2732information.
2733
2734The argument @var{partial-filename} must be a file name containing no
2735directory part and no slash (or backslash on some systems).  The current
2736buffer's default directory is prepended to @var{directory}, if
2737@var{directory} is not absolute.
2738
2739In the following example, suppose that @file{~rms/lewis} is the current
2740default directory, and has five files whose names begin with @samp{f}:
2741@file{foo}, @file{file~}, @file{file.c}, @file{file.c.~1~}, and
2742@file{file.c.~2~}.
2743
2744@example
2745@group
2746(file-name-all-completions "f" "")
2747     @result{} ("foo" "file~" "file.c.~2~"
2748                "file.c.~1~" "file.c")
2749@end group
2750
2751@group
2752(file-name-all-completions "fo" "")
2753     @result{} ("foo")
2754@end group
2755@end example
2756@end defun
2757
2758@defun file-name-completion filename directory &optional predicate
2759This function completes the file name @var{filename} in directory
2760@var{directory}.  It returns the longest prefix common to all file names
2761in directory @var{directory} that start with @var{filename}.  If
2762@var{predicate} is non-@code{nil} then it ignores possible completions
2763that don't satisfy @var{predicate}, after calling that function
2764with one argument, the expanded absolute file name.
2765
2766If only one match exists and @var{filename} matches it exactly, the
2767function returns @code{t}.  The function returns @code{nil} if directory
2768@var{directory} contains no name starting with @var{filename}.
2769
2770In the following example, suppose that the current default directory
2771has five files whose names begin with @samp{f}: @file{foo},
2772@file{file~}, @file{file.c}, @file{file.c.~1~}, and
2773@file{file.c.~2~}.
2774
2775@example
2776@group
2777(file-name-completion "fi" "")
2778     @result{} "file"
2779@end group
2780
2781@group
2782(file-name-completion "file.c.~1" "")
2783     @result{} "file.c.~1~"
2784@end group
2785
2786@group
2787(file-name-completion "file.c.~1~" "")
2788     @result{} t
2789@end group
2790
2791@group
2792(file-name-completion "file.c.~3" "")
2793     @result{} nil
2794@end group
2795@end example
2796@end defun
2797
2798@defopt completion-ignored-extensions
2799@code{file-name-completion} usually ignores file names that end in any
2800string in this list.  It does not ignore them when all the possible
2801completions end in one of these suffixes.  This variable has no effect
2802on @code{file-name-all-completions}.
2803
2804A typical value might look like this:
2805
2806@example
2807@group
2808completion-ignored-extensions
2809     @result{} (".o" ".elc" "~" ".dvi")
2810@end group
2811@end example
2812
2813If an element of @code{completion-ignored-extensions} ends in a slash
2814@samp{/}, it signals a directory.  The elements which do @emph{not} end
2815in a slash will never match a directory; thus, the above value will not
2816filter out a directory named @file{foo.elc}.
2817@end defopt
2818
2819@node Standard File Names
2820@subsection Standard File Names
2821
2822  Sometimes, an Emacs Lisp program needs to specify a standard file
2823name for a particular use---typically, to hold configuration data
2824specified by the current user.  Usually, such files should be located
2825in the directory specified by @code{user-emacs-directory}, which is
2826typically @file{~/.config/emacs/} or @file{~/.emacs.d/} by default (@pxref{Find
2827Init,,How Emacs Finds Your Init File, emacs, The GNU Emacs Manual}).
2828For example, abbrev definitions are stored by default in
2829@file{~/.config/emacs/abbrev_defs} or @file{~/.emacs.d/abbrev_defs}.
2830The easiest way to specify such a file name is to use the function
2831@code{locate-user-emacs-file}.
2832
2833@defun locate-user-emacs-file base-name &optional old-name
2834This function returns an absolute file name for an Emacs-specific
2835configuration or data file.  The argument @file{base-name} should be a
2836relative file name.  The return value is the absolute name of a file
2837in the directory specified by @code{user-emacs-directory}; if that
2838directory does not exist, this function creates it.
2839
2840If the optional argument @var{old-name} is non-@code{nil}, it
2841specifies a file in the user's home directory,
2842@file{~/@var{old-name}}.  If such a file exists, the return value is
2843the absolute name of that file, instead of the file specified by
2844@var{base-name}.  This argument is intended to be used by Emacs
2845packages to provide backward compatibility.  For instance, prior to
2846the introduction of @code{user-emacs-directory}, the abbrev file was
2847located in @file{~/.abbrev_defs}.  Here is the definition of
2848@code{abbrev-file-name}:
2849
2850@example
2851(defcustom abbrev-file-name
2852  (locate-user-emacs-file "abbrev_defs" ".abbrev_defs")
2853  "Default name of file from which to read abbrevs."
2854  @dots{}
2855  :type 'file)
2856@end example
2857@end defun
2858
2859  A lower-level function for standardizing file names, which
2860@code{locate-user-emacs-file} uses as a subroutine, is
2861@code{convert-standard-filename}.
2862
2863@defun convert-standard-filename filename
2864This function returns a file name based on @var{filename}, which fits
2865the conventions of the current operating system.
2866
2867On GNU and other POSIX-like systems, this simply returns @var{filename}.
2868On other operating systems, it may enforce system-specific file name
2869conventions; for example, on MS-DOS this function performs a variety
2870of changes to enforce MS-DOS file name limitations, including
2871converting any leading @samp{.} to @samp{_} and truncating to three
2872characters after the @samp{.}.
2873
2874The recommended way to use this function is to specify a name which
2875fits the conventions of GNU and Unix systems, and pass it to
2876@code{convert-standard-filename}.
2877@end defun
2878
2879@node Contents of Directories
2880@section Contents of Directories
2881@cindex directory-oriented functions
2882@cindex file names in directory
2883
2884  A directory is a kind of file that contains other files entered under
2885various names.  Directories are a feature of the file system.
2886
2887  Emacs can list the names of the files in a directory as a Lisp list,
2888or display the names in a buffer using the @code{ls} shell command.  In
2889the latter case, it can optionally display information about each file,
2890depending on the options passed to the @code{ls} command.
2891
2892@defun directory-files directory &optional full-name match-regexp nosort
2893This function returns a list of the names of the files in the directory
2894@var{directory}.  By default, the list is in alphabetical order.
2895
2896If @var{full-name} is non-@code{nil}, the function returns the files'
2897absolute file names.  Otherwise, it returns the names relative to
2898the specified directory.
2899
2900If @var{match-regexp} is non-@code{nil}, this function returns only
2901those file names that contain a match for that regular expression---the
2902other file names are excluded from the list.  On case-insensitive
2903filesystems, the regular expression matching is case-insensitive.
2904
2905@c Emacs 19 feature
2906If @var{nosort} is non-@code{nil}, @code{directory-files} does not sort
2907the list, so you get the file names in no particular order.  Use this if
2908you want the utmost possible speed and don't care what order the files
2909are processed in.  If the order of processing is visible to the user,
2910then the user will probably be happier if you do sort the names.
2911
2912@example
2913@group
2914(directory-files "~lewis")
2915     @result{} ("#foo#" "#foo.el#" "." ".."
2916         "dired-mods.el" "files.texi"
2917         "files.texi.~1~")
2918@end group
2919@end example
2920
2921An error is signaled if @var{directory} is not the name of a directory
2922that can be read.
2923@end defun
2924
2925@cindex recursive traverse of directory tree
2926@defun directory-files-recursively directory regexp &optional include-directories predicate follow-symlinks
2927Return all files under @var{directory} whose names match @var{regexp}.
2928This function searches the specified @var{directory} and its
2929sub-directories, recursively, for files whose basenames (i.e., without
2930the leading directories) match the specified @var{regexp}, and returns
2931a list of the absolute file names of the matching files
2932(@pxref{Relative File Names, absolute file names}).  The file names
2933are returned in depth-first order, meaning that files in some
2934sub-directory are returned before the files in its parent directory.
2935In addition, matching files found in each subdirectory are sorted
2936alphabetically by their basenames.  By default, directories whose
2937names match @var{regexp} are omitted from the list, but if the
2938optional argument @var{include-directories} is non-@code{nil}, they
2939are included.
2940
2941By default, all subdirectories are descended into.  If @var{predicate}
2942is @code{t}, errors when trying to descend into a subdirectory (for
2943instance, if it's not readable by this user) are ignored.  If it's
2944neither @code{nil} nor @code{t}, it should be a function that takes
2945one parameter (the subdirectory name) and should return non-@code{nil}
2946if the directory is to be descended into.
2947
2948Symbolic links to subdirectories are not followed by default, but if
2949@var{follow-symlinks} is non-@code{nil}, they are followed.
2950@end defun
2951
2952
2953@defun locate-dominating-file file name
2954Starting at @var{file}, go up the directory tree hierarchy looking for
2955the first directory where @var{name}, a string, exists, and return that
2956directory.  If @var{file} is a file, its directory will serve as the
2957starting point for the search; otherwise @var{file} should be a
2958directory from which to start.  The function looks in the starting
2959directory, then in its parent, then in its parent's parent, etc.,
2960until it either finds a directory with @var{name} or reaches the root
2961directory of the filesystem without finding @var{name} -- in the
2962latter case the function returns @code{nil}.
2963
2964The argument @code{name} can also be a predicate function.  The
2965predicate is called for every directory examined by the function,
2966starting from @var{file} (even if @var{file} is not a directory).  It
2967is called with one argument (the file or directory) and should return
2968non-@code{nil} if that directory is the one it is looking for.
2969@end defun
2970
2971@defun directory-files-and-attributes directory &optional full-name match-regexp nosort id-format
2972This is similar to @code{directory-files} in deciding which files
2973to report on and how to report their names.  However, instead
2974of returning a list of file names, it returns for each file a
2975list @code{(@var{filename} . @var{attributes})}, where @var{attributes}
2976is what @code{file-attributes} returns for that file.
2977The optional argument @var{id-format} has the same meaning as the
2978corresponding argument to @code{file-attributes} (@pxref{Definition
2979of file-attributes}).
2980@end defun
2981
2982@defun file-expand-wildcards pattern &optional full
2983This function expands the wildcard pattern @var{pattern}, returning
2984a list of file names that match it.
2985
2986If @var{pattern} is written as an absolute file name,
2987the values are absolute also.
2988
2989If @var{pattern} is written as a relative file name, it is interpreted
2990relative to the current default directory.  The file names returned are
2991normally also relative to the current default directory.  However, if
2992@var{full} is non-@code{nil}, they are absolute.
2993@end defun
2994
2995@defun insert-directory file switches &optional wildcard full-directory-p
2996This function inserts (in the current buffer) a directory listing for
2997directory @var{file}, formatted with @code{ls} according to
2998@var{switches}.  It leaves point after the inserted text.
2999@var{switches} may be a string of options, or a list of strings
3000representing individual options.
3001
3002The argument @var{file} may be either a directory or a file
3003specification including wildcard characters.  If @var{wildcard} is
3004non-@code{nil}, that means treat @var{file} as a file specification with
3005wildcards.
3006
3007If @var{full-directory-p} is non-@code{nil}, that means the directory
3008listing is expected to show the full contents of a directory.  You
3009should specify @code{t} when @var{file} is a directory and switches do
3010not contain @samp{-d}.  (The @samp{-d} option to @code{ls} says to
3011describe a directory itself as a file, rather than showing its
3012contents.)
3013
3014On most systems, this function works by running a directory listing
3015program whose name is in the variable @code{insert-directory-program}.
3016If @var{wildcard} is non-@code{nil}, it also runs the shell specified by
3017@code{shell-file-name}, to expand the wildcards.
3018
3019MS-DOS and MS-Windows systems usually lack the standard Unix program
3020@code{ls}, so this function emulates the standard Unix program @code{ls}
3021with Lisp code.
3022
3023As a technical detail, when @var{switches} contains the long
3024@samp{--dired} option, @code{insert-directory} treats it specially,
3025for the sake of dired.  However, the normally equivalent short
3026@samp{-D} option is just passed on to @code{insert-directory-program},
3027as any other option.
3028@end defun
3029
3030@defvar insert-directory-program
3031This variable's value is the program to run to generate a directory listing
3032for the function @code{insert-directory}.  It is ignored on systems
3033which generate the listing with Lisp code.
3034@end defvar
3035
3036@node Create/Delete Dirs
3037@section Creating, Copying and Deleting Directories
3038@cindex creating, copying and deleting directories
3039@c Emacs 19 features
3040
3041  Most Emacs Lisp file-manipulation functions get errors when used on
3042files that are directories.  For example, you cannot delete a directory
3043with @code{delete-file}.  These special functions exist to create and
3044delete directories.
3045
3046@findex mkdir
3047@deffn Command make-directory dirname &optional parents
3048This command creates a directory named @var{dirname}.  If
3049@var{parents} is non-@code{nil}, as is always the case in an
3050interactive call, that means to create the parent directories first,
3051if they don't already exist.
3052@code{mkdir} is an alias for this.
3053@end deffn
3054
3055@deffn Command make-empty-file filename &optional parents
3056This command creates an empty file named @var{filename}.
3057As @code{make-directory}, this command creates parent directories
3058if @var{parents} is non-@code{nil}.
3059If @var{filename} already exists, this command signals an error.
3060@end deffn
3061
3062@deffn Command copy-directory dirname newname &optional keep-time parents copy-contents
3063This command copies the directory named @var{dirname} to
3064@var{newname}.  If @var{newname} is a directory name,
3065@var{dirname} will be copied to a subdirectory there.
3066@xref{Directory Names}.
3067
3068It always sets the file modes of the copied files to match the
3069corresponding original file.
3070
3071The third argument @var{keep-time} non-@code{nil} means to preserve the
3072modification time of the copied files.  A prefix arg makes
3073@var{keep-time} non-@code{nil}.
3074
3075The fourth argument @var{parents} says whether to
3076create parent directories if they don't exist.  Interactively,
3077this happens by default.
3078
3079The fifth argument @var{copy-contents}, if non-@code{nil}, means to
3080copy the contents of @var{dirname} directly into @var{newname} if the
3081latter is a directory name, instead of copying @var{dirname} into
3082it as a subdirectory.
3083@end deffn
3084
3085@cindex trash
3086@vindex delete-by-moving-to-trash
3087@deffn Command delete-directory dirname &optional recursive trash
3088This command deletes the directory named @var{dirname}.  The function
3089@code{delete-file} does not work for files that are directories; you
3090must use @code{delete-directory} for them.  If @var{recursive} is
3091@code{nil}, and the directory contains any files,
3092@code{delete-directory} signals an error.
3093If recursive is non-@code{nil}, there is no error merely because the
3094directory or its files are deleted by some other process before
3095@code{delete-directory} gets to them.
3096
3097@code{delete-directory} only follows symbolic links at the level of
3098parent directories.
3099
3100If the optional argument @var{trash} is non-@code{nil} and the
3101variable @code{delete-by-moving-to-trash} is non-@code{nil}, this
3102command moves the file into the system Trash instead of deleting it.
3103@xref{Misc File Ops,,Miscellaneous File Operations, emacs, The GNU
3104Emacs Manual}.  When called interactively, @var{trash} is @code{t} if
3105no prefix argument is given, and @code{nil} otherwise.
3106@end deffn
3107
3108@node Magic File Names
3109@section Making Certain File Names ``Magic''
3110@cindex magic file names
3111
3112  You can implement special handling for certain file names.  This is
3113called making those names @dfn{magic}.  The principal use for this
3114feature is in implementing access to remote files (@pxref{Remote Files,,
3115Remote Files, emacs, The GNU Emacs Manual}).
3116
3117  To define a kind of magic file name, you must supply a regular
3118expression to define the class of names (all those that match the
3119regular expression), plus a handler that implements all the primitive
3120Emacs file operations for file names that match.
3121
3122@cindex file name handler
3123@vindex file-name-handler-alist
3124  The variable @code{file-name-handler-alist} holds a list of handlers,
3125together with regular expressions that determine when to apply each
3126handler.  Each element has this form:
3127
3128@example
3129(@var{regexp} . @var{handler})
3130@end example
3131
3132@noindent
3133All the Emacs primitives for file access and file name transformation
3134check the given file name against @code{file-name-handler-alist}.  If
3135the file name matches @var{regexp}, the primitives handle that file by
3136calling @var{handler}.
3137
3138  The first argument given to @var{handler} is the name of the
3139primitive, as a symbol; the remaining arguments are the arguments that
3140were passed to that primitive.  (The first of these arguments is most
3141often the file name itself.)  For example, if you do this:
3142
3143@example
3144(file-exists-p @var{filename})
3145@end example
3146
3147@noindent
3148and @var{filename} has handler @var{handler}, then @var{handler} is
3149called like this:
3150
3151@example
3152(funcall @var{handler} 'file-exists-p @var{filename})
3153@end example
3154
3155  When a function takes two or more arguments that must be file names,
3156it checks each of those names for a handler.  For example, if you do
3157this:
3158
3159@example
3160(expand-file-name @var{filename} @var{dirname})
3161@end example
3162
3163@noindent
3164then it checks for a handler for @var{filename} and then for a handler
3165for @var{dirname}.  In either case, the @var{handler} is called like
3166this:
3167
3168@example
3169(funcall @var{handler} 'expand-file-name @var{filename} @var{dirname})
3170@end example
3171
3172@noindent
3173The @var{handler} then needs to figure out whether to handle
3174@var{filename} or @var{dirname}.
3175
3176  If the specified file name matches more than one handler, the one
3177whose match starts last in the file name gets precedence.  This rule
3178is chosen so that handlers for jobs such as uncompression are handled
3179first, before handlers for jobs such as remote file access.
3180
3181  Here are the operations that a magic file name handler gets to handle:
3182
3183@ifnottex
3184@noindent
3185@code{access-file}, @code{add-name-to-file},
3186@code{byte-compiler-base-file-name},@*
3187@code{copy-directory}, @code{copy-file},
3188@code{delete-directory}, @code{delete-file},
3189@code{diff-latest-backup-file},
3190@code{directory-file-name},
3191@code{directory-files},
3192@code{directory-files-and-attributes},
3193@code{dired-compress-file}, @code{dired-uncache},
3194@code{exec-path}, @code{expand-file-name},@*
3195@code{file-accessible-directory-p},
3196@code{file-acl},
3197@code{file-attributes},
3198@code{file-directory-p},
3199@code{file-equal-p},
3200@code{file-executable-p}, @code{file-exists-p},
3201@code{file-in-directory-p},
3202@code{file-local-copy},
3203@code{file-modes}, @code{file-name-all-completions},
3204@code{file-name-as-directory},
3205@code{file-name-case-insensitive-p},
3206@code{file-name-completion},
3207@code{file-name-directory},
3208@code{file-name-nondirectory},
3209@code{file-name-sans-versions}, @code{file-newer-than-file-p},
3210@code{file-notify-add-watch}, @code{file-notify-rm-watch},
3211@code{file-notify-valid-p},
3212@code{file-ownership-preserved-p},
3213@code{file-readable-p}, @code{file-regular-p},
3214@code{file-remote-p}, @code{file-selinux-context},
3215@code{file-symlink-p}, @code{file-system-info},
3216@code{file-truename}, @code{file-writable-p},
3217@code{find-backup-file-name},@*
3218@code{get-file-buffer},
3219@code{insert-directory},
3220@code{insert-file-contents},@*
3221@code{load},
3222@code{make-auto-save-file-name},
3223@code{make-directory},
3224@code{make-directory-internal},
3225@code{make-nearby-temp-file},
3226@code{make-process},
3227@code{make-symbolic-link},@*
3228@code{process-file},
3229@code{rename-file}, @code{set-file-acl}, @code{set-file-modes},
3230@code{set-file-selinux-context}, @code{set-file-times},
3231@code{set-visited-file-modtime}, @code{shell-command},
3232@code{start-file-process},
3233@code{substitute-in-file-name},@*
3234@code{temporary-file-directory},
3235@code{unhandled-file-name-directory},
3236@code{vc-registered},
3237@code{verify-visited-file-modtime},@*
3238@code{write-region}.
3239@end ifnottex
3240@iftex
3241@noindent
3242@flushleft
3243@code{access-file}, @code{add-name-to-file},
3244@code{byte-com@discretionary{}{}{}piler-base-file-name},
3245@code{copy-directory}, @code{copy-file},
3246@code{delete-directory}, @code{delete-file},
3247@code{diff-latest-backup-file},
3248@code{directory-file-name},
3249@code{directory-files},
3250@code{directory-files-and-at@discretionary{}{}{}tributes},
3251@code{dired-compress-file}, @code{dired-uncache},
3252@code{exec-path}, @code{expand-file-name},
3253@code{file-accessible-direc@discretionary{}{}{}tory-p},
3254@code{file-acl},
3255@code{file-attributes},
3256@code{file-direc@discretionary{}{}{}tory-p},
3257@code{file-equal-p},
3258@code{file-executable-p}, @code{file-exists-p},
3259@code{file-in-directory-p},
3260@code{file-local-copy},
3261@code{file-modes}, @code{file-name-all-completions},
3262@code{file-name-as-directory},
3263@code{file-name-case-insensitive-p},
3264@code{file-name-completion},
3265@code{file-name-directory},
3266@code{file-name-nondirec@discretionary{}{}{}tory},
3267@code{file-name-sans-versions}, @code{file-newer-than-file-p},
3268@code{file-notify-add-watch}, @code{file-notify-rm-watch},
3269@code{file-notify-valid-p},
3270@code{file-ownership-pre@discretionary{}{}{}served-p},
3271@code{file-readable-p}, @code{file-regular-p},
3272@code{file-remote-p}, @code{file-selinux-context},
3273@code{file-symlink-p}, @code{file-system-info},
3274@code{file-truename}, @code{file-writable-p},
3275@code{find-backup-file-name},
3276@code{get-file-buffer},
3277@code{insert-directory},
3278@code{insert-file-contents},
3279@code{load},
3280@code{make-auto-save-file-name},
3281@code{make-direc@discretionary{}{}{}tory},
3282@code{make-direc@discretionary{}{}{}tory-internal},
3283@code{make-process},
3284@code{make-symbolic-link},
3285@code{process-file},
3286@code{rename-file}, @code{set-file-acl}, @code{set-file-modes},
3287@code{set-file-selinux-context}, @code{set-file-times},
3288@code{set-visited-file-modtime}, @code{shell-command},
3289@code{start-file-process},
3290@code{substitute-in-file-name},
3291@code{unhandled-file-name-directory},
3292@code{vc-regis@discretionary{}{}{}tered},
3293@code{verify-visited-file-modtime},
3294@code{write-region}.
3295@end flushleft
3296@end iftex
3297
3298  Handlers for @code{insert-file-contents} typically need to clear the
3299buffer's modified flag, with @code{(set-buffer-modified-p nil)}, if the
3300@var{visit} argument is non-@code{nil}.  This also has the effect of
3301unlocking the buffer if it is locked.
3302
3303  The handler function must handle all of the above operations, and
3304possibly others to be added in the future.  It need not implement all
3305these operations itself---when it has nothing special to do for a
3306certain operation, it can reinvoke the primitive, to handle the
3307operation in the usual way.  It should always reinvoke the primitive
3308for an operation it does not recognize.  Here's one way to do this:
3309
3310@smallexample
3311(defun my-file-handler (operation &rest args)
3312  ;; @r{First check for the specific operations}
3313  ;; @r{that we have special handling for.}
3314  (cond ((eq operation 'insert-file-contents) @dots{})
3315        ((eq operation 'write-region) @dots{})
3316        @dots{}
3317        ;; @r{Handle any operation we don't know about.}
3318        (t (let ((inhibit-file-name-handlers
3319                  (cons 'my-file-handler
3320                        (and (eq inhibit-file-name-operation operation)
3321                             inhibit-file-name-handlers)))
3322                 (inhibit-file-name-operation operation))
3323             (apply operation args)))))
3324@end smallexample
3325
3326  When a handler function decides to call the ordinary Emacs primitive for
3327the operation at hand, it needs to prevent the primitive from calling
3328the same handler once again, thus leading to an infinite recursion.  The
3329example above shows how to do this, with the variables
3330@code{inhibit-file-name-handlers} and
3331@code{inhibit-file-name-operation}.  Be careful to use them exactly as
3332shown above; the details are crucial for proper behavior in the case of
3333multiple handlers, and for operations that have two file names that may
3334each have handlers.
3335
3336@kindex safe-magic @r{(property)}
3337  Handlers that don't really do anything special for actual access to the
3338file---such as the ones that implement completion of host names for
3339remote file names---should have a non-@code{nil} @code{safe-magic}
3340property.  For instance, Emacs normally protects directory names
3341@c FIXME I don't think this means the PATH environment variable?
3342it finds in @code{PATH} from becoming magic, if they look like magic
3343file names, by prefixing them with @samp{/:}.  But if the handler that
3344would be used for them has a non-@code{nil} @code{safe-magic}
3345property, the @samp{/:} is not added.
3346
3347@kindex operations @r{(property)}
3348  A file name handler can have an @code{operations} property to
3349declare which operations it handles in a nontrivial way.  If this
3350property has a non-@code{nil} value, it should be a list of
3351operations; then only those operations will call the handler.  This
3352avoids inefficiency, but its main purpose is for autoloaded handler
3353functions, so that they won't be loaded except when they have real
3354work to do.
3355
3356  Simply deferring all operations to the usual primitives does not
3357work.  For instance, if the file name handler applies to
3358@code{file-exists-p}, then it must handle @code{load} itself, because
3359the usual @code{load} code won't work properly in that case.  However,
3360if the handler uses the @code{operations} property to say it doesn't
3361handle @code{file-exists-p}, then it need not handle @code{load}
3362nontrivially.
3363
3364@defvar inhibit-file-name-handlers
3365This variable holds a list of handlers whose use is presently inhibited
3366for a certain operation.
3367@end defvar
3368
3369@defvar inhibit-file-name-operation
3370The operation for which certain handlers are presently inhibited.
3371@end defvar
3372
3373@defun find-file-name-handler file operation
3374This function returns the handler function for file name @var{file},
3375or @code{nil} if there is none.  The argument @var{operation} should
3376be the operation to be performed on the file---the value you will pass
3377to the handler as its first argument when you call it.  If
3378@var{operation} equals @code{inhibit-file-name-operation}, or if it is
3379not found in the @code{operations} property of the handler, this
3380function returns @code{nil}.
3381@end defun
3382
3383@defun file-local-copy filename
3384This function copies file @var{filename} to an ordinary non-magic file
3385on the local machine, if it isn't on the local machine already.  Magic
3386file names should handle the @code{file-local-copy} operation if they
3387refer to files on other machines.  A magic file name that is used for
3388other purposes than remote file access should not handle
3389@code{file-local-copy}; then this function will treat the file as
3390local.
3391
3392If @var{filename} is local, whether magic or not, this function does
3393nothing and returns @code{nil}.  Otherwise it returns the file name
3394of the local copy file.
3395@end defun
3396
3397@defun file-remote-p filename &optional identification connected
3398This function tests whether @var{filename} is a remote file.  If
3399@var{filename} is local (not remote), the return value is @code{nil}.
3400If @var{filename} is indeed remote, the return value is a string that
3401identifies the remote system.
3402
3403This identifier string can include a host name and a user name, as
3404well as characters designating the method used to access the remote
3405system.  For example, the remote identifier string for the filename
3406@code{/sudo::/some/file} is @code{/sudo:root@@localhost:}.
3407
3408If @code{file-remote-p} returns the same identifier for two different
3409filenames, that means they are stored on the same file system and can
3410be accessed locally with respect to each other.  This means, for
3411example, that it is possible to start a remote process accessing both
3412files at the same time.  Implementers of file name handlers need to
3413ensure this principle is valid.
3414
3415@var{identification} specifies which part of the identifier shall be
3416returned as string.  @var{identification} can be the symbol
3417@code{method}, @code{user} or @code{host}; any other value is handled
3418like @code{nil} and means to return the complete identifier string.
3419In the example above, the remote @code{user} identifier string would
3420be @code{root}.
3421
3422If @var{connected} is non-@code{nil}, this function returns @code{nil}
3423even if @var{filename} is remote, if Emacs has no network connection
3424to its host.  This is useful when you want to avoid the delay of
3425making connections when they don't exist.
3426@end defun
3427
3428@defun unhandled-file-name-directory filename
3429This function returns the name of a directory that is not magic.  For
3430a non-magic @var{filename} it returns the corresponding directory name
3431(@pxref{Directory Names}).  For a magic @var{filename}, it invokes the
3432file name handler, which therefore decides what value to return.  If
3433@var{filename} is not accessible from a local process, then the file
3434name handler should indicate that by returning @code{nil}.
3435
3436This is useful for running a subprocess; every subprocess must have a
3437non-magic directory to serve as its current directory, and this function
3438is a good way to come up with one.
3439@end defun
3440
3441@cindex local part of remote file name
3442@defun file-local-name filename
3443This function returns the @dfn{local part} of @var{filename}.  This is
3444the part of the file's name that identifies it on the remote host, and
3445is typically obtained by removing from the remote file name the parts
3446that specify the remote host and the method of accessing it.  For
3447example:
3448
3449@smallexample
3450(file-local-name "/ssh:@var{user}@@@var{host}:/foo/bar")
3451     @result{} "/foo/bar"
3452@end smallexample
3453
3454For a remote @var{filename}, this function returns a file name which
3455could be used directly as an argument of a remote process
3456(@pxref{Asynchronous Processes}, and @pxref{Synchronous Processes}),
3457and as the program to run on the remote host.  If @var{filename} is
3458local, this function returns it unchanged.
3459@end defun
3460
3461@defopt remote-file-name-inhibit-cache
3462The attributes of remote files can be cached for better performance.  If
3463they are changed outside of Emacs's control, the cached values become
3464invalid, and must be reread.
3465
3466When this variable is set to @code{nil}, cached values are never
3467expired.  Use this setting with caution, only if you are sure nothing
3468other than Emacs ever changes the remote files.  If it is set to
3469@code{t}, cached values are never used.  This is the safest value, but
3470could result in performance degradation.
3471
3472A compromise is to set it to a positive number.  This means that
3473cached values are used for that amount of seconds since they were
3474cached.  If a remote file is checked regularly, it might be a good
3475idea to let-bind this variable to a value less than the time period
3476between consecutive checks.  For example:
3477
3478@example
3479(defun display-time-file-nonempty-p (file)
3480  (let ((remote-file-name-inhibit-cache
3481         (- display-time-interval 5)))
3482    (and (file-exists-p file)
3483         (< 0 (file-attribute-size
3484               (file-attributes
3485                (file-chase-links file)))))))
3486@end example
3487@end defopt
3488
3489@node Format Conversion
3490@section File Format Conversion
3491
3492@cindex file format conversion
3493@cindex encoding file formats
3494@cindex decoding file formats
3495@cindex text properties in files
3496@cindex saving text properties
3497  Emacs performs several steps to convert the data in a buffer (text,
3498text properties, and possibly other information) to and from a
3499representation suitable for storing into a file.  This section describes
3500the fundamental functions that perform this @dfn{format conversion},
3501namely @code{insert-file-contents} for reading a file into a buffer,
3502and @code{write-region} for writing a buffer into a file.
3503
3504@menu
3505* Overview: Format Conversion Overview.     @code{insert-file-contents} and @code{write-region}.
3506* Round-Trip: Format Conversion Round-Trip. Using @code{format-alist}.
3507* Piecemeal: Format Conversion Piecemeal.   Specifying non-paired conversion.
3508@end menu
3509
3510@node Format Conversion Overview
3511@subsection Overview
3512@noindent
3513The function @code{insert-file-contents}:
3514
3515@itemize
3516@item initially, inserts bytes from the file into the buffer;
3517@item decodes bytes to characters as appropriate;
3518@item processes formats as defined by entries in @code{format-alist}; and
3519@item calls functions in @code{after-insert-file-functions}.
3520@end itemize
3521
3522@noindent
3523The function @code{write-region}:
3524
3525@itemize
3526@item initially, calls functions in @code{write-region-annotate-functions};
3527@item processes formats as defined by entries in @code{format-alist};
3528@item encodes characters to bytes as appropriate; and
3529@item modifies the file with the bytes.
3530@end itemize
3531
3532  This shows the symmetry of the lowest-level operations; reading and
3533writing handle things in opposite order.  The rest of this section
3534describes the two facilities surrounding the three variables named
3535above, as well as some related functions.  @ref{Coding Systems}, for
3536details on character encoding and decoding.
3537
3538@node Format Conversion Round-Trip
3539@subsection Round-Trip Specification
3540
3541  The most general of the two facilities is controlled by the variable
3542@code{format-alist}, a list of @dfn{file format} specifications, which
3543describe textual representations used in files for the data in an Emacs
3544buffer.  The descriptions for reading and writing are paired, which is
3545why we call this ``round-trip'' specification
3546(@pxref{Format Conversion Piecemeal}, for non-paired specification).
3547
3548@defvar format-alist
3549This list contains one format definition for each defined file format.
3550Each format definition is a list of this form:
3551
3552@example
3553(@var{name} @var{doc-string} @var{regexp} @var{from-fn} @var{to-fn} @var{modify} @var{mode-fn} @var{preserve})
3554@end example
3555@end defvar
3556
3557@cindex format definition
3558@noindent
3559Here is what the elements in a format definition mean:
3560
3561@table @var
3562@item name
3563The name of this format.
3564
3565@item doc-string
3566A documentation string for the format.
3567
3568@item regexp
3569A regular expression which is used to recognize files represented in
3570this format.  If @code{nil}, the format is never applied automatically.
3571
3572@item from-fn
3573A shell command or function to decode data in this format (to convert
3574file data into the usual Emacs data representation).
3575
3576A shell command is represented as a string; Emacs runs the command as a
3577filter to perform the conversion.
3578
3579If @var{from-fn} is a function, it is called with two arguments, @var{begin}
3580and @var{end}, which specify the part of the buffer it should convert.
3581It should convert the text by editing it in place.  Since this can
3582change the length of the text, @var{from-fn} should return the modified
3583end position.
3584
3585One responsibility of @var{from-fn} is to make sure that the beginning
3586of the file no longer matches @var{regexp}.  Otherwise it is likely to
3587get called again.  Also, @var{from-fn} must not involve buffers or
3588files other than the one being decoded, otherwise the internal buffer
3589used for formatting might be overwritten.
3590
3591@item to-fn
3592A shell command or function to encode data in this format---that is, to
3593convert the usual Emacs data representation into this format.
3594
3595If @var{to-fn} is a string, it is a shell command; Emacs runs the
3596command as a filter to perform the conversion.
3597
3598If @var{to-fn} is a function, it is called with three arguments:
3599@var{begin} and @var{end}, which specify the part of the buffer it
3600should convert, and @var{buffer}, which specifies which buffer.  There
3601are two ways it can do the conversion:
3602
3603@itemize @bullet
3604@item
3605By editing the buffer in place.  In this case, @var{to-fn} should
3606return the end-position of the range of text, as modified.
3607
3608@item
3609By returning a list of annotations.  This is a list of elements of the
3610form @code{(@var{position} . @var{string})}, where @var{position} is an
3611integer specifying the relative position in the text to be written, and
3612@var{string} is the annotation to add there.  The list must be sorted in
3613order of position when @var{to-fn} returns it.
3614
3615When @code{write-region} actually writes the text from the buffer to the
3616file, it intermixes the specified annotations at the corresponding
3617positions.  All this takes place without modifying the buffer.
3618@end itemize
3619
3620@var{to-fn} must not involve buffers or files other than the one being
3621encoded, otherwise the internal buffer used for formatting might be
3622overwritten.
3623
3624@item modify
3625A flag, @code{t} if the encoding function modifies the buffer, and
3626@code{nil} if it works by returning a list of annotations.
3627
3628@item mode-fn
3629A minor-mode function to call after visiting a file converted from this
3630format.  The function is called with one argument, the integer 1;
3631that tells a minor-mode function to enable the mode.
3632
3633@item preserve
3634A flag, @code{t} if @code{format-write-file} should not remove this format
3635from @code{buffer-file-format}.
3636@end table
3637
3638The function @code{insert-file-contents} automatically recognizes file
3639formats when it reads the specified file.  It checks the text of the
3640beginning of the file against the regular expressions of the format
3641definitions, and if it finds a match, it calls the decoding function for
3642that format.  Then it checks all the known formats over again.
3643It keeps checking them until none of them is applicable.
3644
3645Visiting a file, with @code{find-file-noselect} or the commands that use
3646it, performs conversion likewise (because it calls
3647@code{insert-file-contents}); it also calls the mode function for each
3648format that it decodes.  It stores a list of the format names in the
3649buffer-local variable @code{buffer-file-format}.
3650
3651@defvar buffer-file-format
3652This variable states the format of the visited file.  More precisely,
3653this is a list of the file format names that were decoded in the course
3654of visiting the current buffer's file.  It is always buffer-local in all
3655buffers.
3656@end defvar
3657
3658When @code{write-region} writes data into a file, it first calls the
3659encoding functions for the formats listed in @code{buffer-file-format},
3660in the order of appearance in the list.
3661
3662@deffn Command format-write-file file format &optional confirm
3663This command writes the current buffer contents into the file @var{file}
3664in a format based on @var{format}, which is a list of format names.  It
3665constructs the actual format starting from @var{format}, then appending
3666any elements from the value of @code{buffer-file-format} with a
3667non-@code{nil} @var{preserve} flag (see above), if they are not already
3668present in @var{format}.  It then updates @code{buffer-file-format} with
3669this format, making it the default for future saves.  Except for the
3670@var{format} argument, this command is similar to @code{write-file}.  In
3671particular, @var{confirm} has the same meaning and interactive treatment
3672as the corresponding argument to @code{write-file}.  @xref{Definition of
3673write-file}.
3674@end deffn
3675
3676@deffn Command format-find-file file format
3677This command finds the file @var{file}, converting it according to
3678format @var{format}.  It also makes @var{format} the default if the
3679buffer is saved later.
3680
3681The argument @var{format} is a list of format names.  If @var{format} is
3682@code{nil}, no conversion takes place.  Interactively, typing just
3683@key{RET} for @var{format} specifies @code{nil}.
3684@end deffn
3685
3686@deffn Command format-insert-file file format &optional beg end
3687This command inserts the contents of file @var{file}, converting it
3688according to format @var{format}.  If @var{beg} and @var{end} are
3689non-@code{nil}, they specify which part of the file to read, as in
3690@code{insert-file-contents} (@pxref{Reading from Files}).
3691
3692The return value is like what @code{insert-file-contents} returns: a
3693list of the absolute file name and the length of the data inserted
3694(after conversion).
3695
3696The argument @var{format} is a list of format names.  If @var{format} is
3697@code{nil}, no conversion takes place.  Interactively, typing just
3698@key{RET} for @var{format} specifies @code{nil}.
3699@end deffn
3700
3701@defvar buffer-auto-save-file-format
3702This variable specifies the format to use for auto-saving.  Its value is
3703a list of format names, just like the value of
3704@code{buffer-file-format}; however, it is used instead of
3705@code{buffer-file-format} for writing auto-save files.  If the value
3706is @code{t}, the default, auto-saving uses the same format as a
3707regular save in the same buffer.  This variable is always buffer-local
3708in all buffers.
3709@end defvar
3710
3711@node Format Conversion Piecemeal
3712@subsection Piecemeal Specification
3713
3714  In contrast to the round-trip specification described in the previous
3715subsection (@pxref{Format Conversion Round-Trip}), you can use the variables
3716@code{after-insert-file-functions} and @code{write-region-annotate-functions}
3717to separately control the respective reading and writing conversions.
3718
3719  Conversion starts with one representation and produces another
3720representation.  When there is only one conversion to do, there is no
3721conflict about what to start with.  However, when there are multiple
3722conversions involved, conflict may arise when two conversions need to
3723start with the same data.
3724
3725  This situation is best understood in the context of converting text
3726properties during @code{write-region}.  For example, the character at
3727position 42 in a buffer is @samp{X} with a text property @code{foo}.  If
3728the conversion for @code{foo} is done by inserting into the buffer, say,
3729@samp{FOO:}, then that changes the character at position 42 from
3730@samp{X} to @samp{F}.  The next conversion will start with the wrong
3731data straight away.
3732
3733  To avoid conflict, cooperative conversions do not modify the buffer,
3734but instead specify @dfn{annotations}, a list of elements of the form
3735@code{(@var{position} . @var{string})}, sorted in order of increasing
3736@var{position}.
3737
3738  If there is more than one conversion, @code{write-region} merges their
3739annotations destructively into one sorted list.  Later, when the text
3740from the buffer is actually written to the file, it intermixes the
3741specified annotations at the corresponding positions.  All this takes
3742place without modifying the buffer.
3743
3744@c ??? What about "overriding" conversions like those allowed
3745@c ??? for 'write-region-annotate-functions', below?  --ttn
3746
3747  In contrast, when reading, the annotations intermixed with the text
3748are handled immediately.  @code{insert-file-contents} sets point to
3749the beginning of some text to be converted, then calls the conversion
3750functions with the length of that text.  These functions should always
3751return with point at the beginning of the inserted text.  This
3752approach makes sense for reading because annotations removed by the
3753first converter can't be mistakenly processed by a later converter.
3754Each conversion function should scan for the annotations it
3755recognizes, remove the annotation, modify the buffer text (to set a
3756text property, for example), and return the updated length of the
3757text, as it stands after those changes.  The value returned by one
3758function becomes the argument to the next function.
3759
3760@defvar write-region-annotate-functions
3761A list of functions for @code{write-region} to call.  Each function in
3762the list is called with two arguments: the start and end of the region
3763to be written.  These functions should not alter the contents of the
3764buffer.  Instead, they should return annotations.
3765
3766As a special case, a function may return with a different buffer
3767current.  Emacs takes this to mean that the current buffer contains
3768altered text to be output.  It therefore changes the @var{start} and
3769@var{end} arguments of the @code{write-region} call, giving them the
3770values of @code{point-min} and @code{point-max} in the new buffer,
3771respectively.  It also discards all previous annotations, because they
3772should have been dealt with by this function.
3773@end defvar
3774
3775@defvar write-region-post-annotation-function
3776The value of this variable, if non-@code{nil}, should be a function.
3777This function is called, with no arguments, after @code{write-region}
3778has completed.
3779
3780If any function in @code{write-region-annotate-functions} returns with
3781a different buffer current, Emacs calls
3782@code{write-region-post-annotation-function} more than once.  Emacs
3783calls it with the last buffer that was current, and again with the
3784buffer before that, and so on back to the original buffer.
3785
3786Thus, a function in @code{write-region-annotate-functions} can create
3787a buffer, give this variable the local value of @code{kill-buffer} in
3788that buffer, set up the buffer with altered text, and make the buffer
3789current.  The buffer will be killed after @code{write-region} is done.
3790@end defvar
3791
3792@defvar after-insert-file-functions
3793Each function in this list is called by @code{insert-file-contents}
3794with one argument, the number of characters inserted, and with point
3795at the beginning of the inserted text.  Each function should leave
3796point unchanged, and return the new character count describing the
3797inserted text as modified by the function.
3798@c ??? The docstring mentions a handler from 'file-name-handler-alist'
3799@c     "intercepting" 'insert-file-contents'.  Hmmm.  --ttn
3800@end defvar
3801
3802  We invite users to write Lisp programs to store and retrieve text
3803properties in files, using these hooks, and thus to experiment with
3804various data formats and find good ones.  Eventually we hope users
3805will produce good, general extensions we can install in Emacs.
3806
3807  We suggest not trying to handle arbitrary Lisp objects as text property
3808names or values---because a program that general is probably difficult
3809to write, and slow.  Instead, choose a set of possible data types that
3810are reasonably flexible, and not too hard to encode.
3811