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