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