comparison lispref/files.texi @ 21007:66d807bdc5b4

*** empty log message ***
author Richard M. Stallman <rms@gnu.org>
date Sat, 28 Feb 1998 01:53:53 +0000
parents eb712b69e495
children 90da2489c498
comparison
equal deleted inserted replaced
21006:00022857f529 21007:66d807bdc5b4
1 @c -*-texinfo-*- 1 @c -*-texinfo-*-
2 @c This is part of the GNU Emacs Lisp Reference Manual. 2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc. 3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998 Free Software Foundation, Inc.
4 @c See the file elisp.texi for copying conditions. 4 @c See the file elisp.texi for copying conditions.
5 @setfilename ../info/files 5 @setfilename ../info/files
6 @node Files, Backups and Auto-Saving, Documentation, Top 6 @node Files, Backups and Auto-Saving, Documentation, Top
7 @comment node-name, next, previous, up 7 @comment node-name, next, previous, up
8 @chapter Files 8 @chapter Files
26 * Reading from Files:: Reading files into buffers without visiting. 26 * Reading from Files:: Reading files into buffers without visiting.
27 * Writing to Files:: Writing new files from parts of buffers. 27 * Writing to Files:: Writing new files from parts of buffers.
28 * File Locks:: Locking and unlocking files, to prevent 28 * File Locks:: Locking and unlocking files, to prevent
29 simultaneous editing by two people. 29 simultaneous editing by two people.
30 * Information about Files:: Testing existence, accessibility, size of files. 30 * Information about Files:: Testing existence, accessibility, size of files.
31 * Changing File Attributes:: Renaming files, changing protection, etc. 31 * Changing Files:: Renaming files, changing protection, etc.
32 * File Names:: Decomposing and expanding file names. 32 * File Names:: Decomposing and expanding file names.
33 * Contents of Directories:: Getting a list of the files in a directory. 33 * Contents of Directories:: Getting a list of the files in a directory.
34 * Create/Delete Dirs:: Creating and Deleting Directories. 34 * Create/Delete Dirs:: Creating and Deleting Directories.
35 * Magic File Names:: Defining "magic" special handling 35 * Magic File Names:: Defining "magic" special handling
36 for certain file names. 36 for certain file names.
101 101
102 When @code{find-file} is called interactively, it prompts for 102 When @code{find-file} is called interactively, it prompts for
103 @var{filename} in the minibuffer. 103 @var{filename} in the minibuffer.
104 @end deffn 104 @end deffn
105 105
106 @defun find-file-noselect filename 106 @defun find-file-noselect filename &optional nowarn rawfile
107 This function is the guts of all the file-visiting functions. It finds 107 This function is the guts of all the file-visiting functions. It finds
108 or creates a buffer visiting the file @var{filename}, and returns it. 108 or creates a buffer visiting the file @var{filename}, and returns it.
109 It uses an existing buffer if there is one, and otherwise creates a new 109 It uses an existing buffer if there is one, and otherwise creates a new
110 buffer and reads the file into it. You may make the buffer current or 110 buffer and reads the file into it. You may make the buffer current or
111 display it in a window if you wish, but this function does not do so. 111 display it in a window if you wish, but this function does not do so.
118 118
119 If @code{find-file-noselect} needs to create a buffer, and there is no 119 If @code{find-file-noselect} needs to create a buffer, and there is no
120 file named @var{filename}, it displays the message @samp{New file} in 120 file named @var{filename}, it displays the message @samp{New file} in
121 the echo area, and leaves the buffer empty. 121 the echo area, and leaves the buffer empty.
122 122
123 The @code{find-file-noselect} function calls @code{after-find-file} 123 This function displays warning or advisory messages in various peculiar
124 after reading the file (@pxref{Subroutines of Visiting}). That function 124 cases, unless the optional argument @var{nowarn} is non-@code{nil}.
125 sets the buffer major mode, parses local variables, warns the user if 125
126 there exists an auto-save file more recent than the file just visited, 126 The @code{find-file-noselect} function normally calls
127 and finishes by running the functions in @code{find-file-hooks}. 127 @code{after-find-file} after reading the file (@pxref{Subroutines of
128 Visiting}). That function sets the buffer major mode, parses local
129 variables, warns the user if there exists an auto-save file more recent
130 than the file just visited, and finishes by running the functions in
131 @code{find-file-hooks}.
132
133 If the optional argument @var{rawfile} is non-@code{nil}, then
134 @code{after-find-file} is not called, and the
135 @code{find-file-not-found-hooks} are not run in case of failure. What's
136 more, a non-@code{nil} @var{rawfile} value suppresses coding system
137 conversion (@pxref{Coding Systems}) and format conversion (@pxref{Format
138 Conversion}).
128 139
129 The @code{find-file-noselect} function returns the buffer that is 140 The @code{find-file-noselect} function returns the buffer that is
130 visiting the file @var{filename}. 141 visiting the file @var{filename}.
131 142
132 @example 143 @example
183 it detects a nonexistent file. It calls them in the order of the list, 194 it detects a nonexistent file. It calls them in the order of the list,
184 until one of them returns non-@code{nil}. @code{buffer-file-name} is 195 until one of them returns non-@code{nil}. @code{buffer-file-name} is
185 already set up. 196 already set up.
186 197
187 This is not a normal hook because the values of the functions are 198 This is not a normal hook because the values of the functions are
188 used and they may not all be called. 199 used, and in many cases only some of the functions are called.
189 @end defvar 200 @end defvar
190 201
191 @node Subroutines of Visiting 202 @node Subroutines of Visiting
192 @comment node-name, next, previous, up 203 @comment node-name, next, previous, up
193 @subsection Subroutines of Visiting 204 @subsection Subroutines of Visiting
329 340
330 You might wish to save the file modes value returned by 341 You might wish to save the file modes value returned by
331 @code{backup-buffer} and use that to set the mode bits of the file that 342 @code{backup-buffer} and use that to set the mode bits of the file that
332 you write. This is what @code{save-buffer} normally does. 343 you write. This is what @code{save-buffer} normally does.
333 344
345 Do not make this variable buffer-local. To set up buffer-specific hook
346 functions, use @code{write-contents-hooks} instead.
347
334 Even though this is not a normal hook, you can use @code{add-hook} and 348 Even though this is not a normal hook, you can use @code{add-hook} and
335 @code{remove-hook} to manipulate the list. @xref{Hooks}. 349 @code{remove-hook} to manipulate the list. @xref{Hooks}.
336 @end defvar 350 @end defvar
337 351
338 @c Emacs 19 feature 352 @c Emacs 19 feature
339 @defvar local-write-file-hooks 353 @defvar local-write-file-hooks
340 This works just like @code{write-file-hooks}, but it is intended 354 This works just like @code{write-file-hooks}, but it is intended to be
341 to be made local to particular buffers. It's not a good idea to make 355 made local to particular buffers, and used for hooks that pertain to the
342 @code{write-file-hooks} local to a buffer---use this variable instead. 356 file name or the way the buffer contents were obtained.
343 357
344 The variable is marked as a permanent local, so that changing the major 358 The variable is marked as a permanent local, so that changing the major
345 mode does not alter a buffer-local value. This is convenient for 359 mode does not alter a buffer-local value. This is convenient for
346 packages that read ``file'' contents in special ways, and set up hooks 360 packages that read ``file'' contents in special ways, and set up hooks
347 to save the data in a corresponding way. 361 to save the data in a corresponding way.
447 If the argument @var{replace} is non-@code{nil}, it means to replace the 461 If the argument @var{replace} is non-@code{nil}, it means to replace the
448 contents of the buffer (actually, just the accessible portion) with the 462 contents of the buffer (actually, just the accessible portion) with the
449 contents of the file. This is better than simply deleting the buffer 463 contents of the file. This is better than simply deleting the buffer
450 contents and inserting the whole file, because (1) it preserves some 464 contents and inserting the whole file, because (1) it preserves some
451 marker positions and (2) it puts less data in the undo list. 465 marker positions and (2) it puts less data in the undo list.
466
467 It works to read a special file with @code{insert-file-contents}
468 as long as @var{replace} and @var{visit} are @code{nil}.
469 @end defun
470
471 @tindex insert-file-contents-literally
472 @defun insert-file-contents-literally filename &optional visit beg end replace
473 This function works like @code{insert-file-contents} except that it does
474 not do format decoding (@pxref{Format Conversion}), does not do
475 character code conversion (@pxref{Coding Systems}), does not run
476 @code{find-file-hooks}, does not perform automatic uncompression, and so
477 on.
452 @end defun 478 @end defun
453 479
454 If you want to pass a file name to another process so that another 480 If you want to pass a file name to another process so that another
455 program can read the file, use the function @code{file-local-copy}; see 481 program can read the file, use the function @code{file-local-copy}; see
456 @ref{Magic File Names}. 482 @ref{Magic File Names}.
512 nor @code{nil} nor a string, then this message is inhibited. This 538 nor @code{nil} nor a string, then this message is inhibited. This
513 feature is useful for programs that use files for internal purposes, 539 feature is useful for programs that use files for internal purposes,
514 files that the user does not need to know about. 540 files that the user does not need to know about.
515 @end deffn 541 @end deffn
516 542
543 @tindex with-temp-file
544 @defmac with-temp-file file body...
545 The @code{with-temp-file} macro evaluates the @var{body} forms
546 with a temporary buffer as the current buffer; then, at the end, it
547 writes the buffer contents into file @var{file}. It kills the temporary
548 buffer when finished, restoring the buffer that was current before the
549 @code{with-temp-file} form.
550
551 The return value is the value of the last form in @var{body}. You can
552 return the contents of the temporary buffer by using
553 @code{(buffer-string)} as the last form.
554
555 The current buffer is restored even in case of an abnormal exit via
556 @code{throw} or error (@pxref{Nonlocal Exits}).
557
558 See also @code{with-temp-buffer} in @ref{Current Buffer}.
559 @end defmac
560
517 @node File Locks 561 @node File Locks
518 @section File Locks 562 @section File Locks
519 @cindex file locks 563 @cindex file locks
520 564
521 When two users edit the same file at the same time, they are likely to 565 When two users edit the same file at the same time, they are likely to
560 does nothing if the current buffer is not visiting a file. 604 does nothing if the current buffer is not visiting a file.
561 @end defun 605 @end defun
562 606
563 @defun ask-user-about-lock file other-user 607 @defun ask-user-about-lock file other-user
564 This function is called when the user tries to modify @var{file}, but it 608 This function is called when the user tries to modify @var{file}, but it
565 is locked by another user named @var{other-user}. The value it returns 609 is locked by another user named @var{other-user}. The default
566 determines what happens next: 610 definition of this function asks the user to say what to do. The value
611 this function returns determines what Emacs does next:
567 612
568 @itemize @bullet 613 @itemize @bullet
569 @item 614 @item
570 A value of @code{t} says to grab the lock on the file. Then 615 A value of @code{t} says to grab the lock on the file. Then
571 this user may edit the file and @var{other-user} loses the lock. 616 this user may edit the file and @var{other-user} loses the lock.
588 @noindent 633 @noindent
589 where @code{file} is the name of the file and @var{other-user} is the 634 where @code{file} is the name of the file and @var{other-user} is the
590 name of the user who has locked the file. 635 name of the user who has locked the file.
591 @end itemize 636 @end itemize
592 637
593 The default definition of this function asks the user to choose what 638 If you wish, you can replace the @code{ask-user-about-lock} function
594 to do. If you wish, you can replace the @code{ask-user-about-lock} 639 with your own version that makes the decision in another way. The code
595 function with your own version that decides in another way. The code
596 for its usual definition is in @file{userlock.el}. 640 for its usual definition is in @file{userlock.el}.
597 @end defun 641 @end defun
598 642
599 @node Information about Files 643 @node Information about Files
600 @section Information about Files 644 @section Information about Files
692 @defun file-accessible-directory-p dirname 736 @defun file-accessible-directory-p dirname
693 This function returns @code{t} if you have permission to open existing 737 This function returns @code{t} if you have permission to open existing
694 files in the directory whose name as a file is @var{dirname}; otherwise 738 files in the directory whose name as a file is @var{dirname}; otherwise
695 (or if there is no such directory), it returns @code{nil}. The value 739 (or if there is no such directory), it returns @code{nil}. The value
696 of @var{dirname} may be either a directory name or the file name of a 740 of @var{dirname} may be either a directory name or the file name of a
697 directory. 741 file which is a directory.
698 742
699 Example: after the following, 743 Example: after the following,
700 744
701 @example 745 @example
702 (file-accessible-directory-p "/foo") 746 (file-accessible-directory-p "/foo")
704 @end example 748 @end example
705 749
706 @noindent 750 @noindent
707 we can deduce that any attempt to read a file in @file{/foo/} will 751 we can deduce that any attempt to read a file in @file{/foo/} will
708 give an error. 752 give an error.
753 @end defun
754
755 @tindex access-file
756 @defun access-file filename string
757 This function opens file @var{filename} for reading, then closes it and
758 returns @code{nil}. However, if the open fails, it signals an error
759 using @var{string} as the error message text.
709 @end defun 760 @end defun
710 761
711 @defun file-ownership-preserved-p filename 762 @defun file-ownership-preserved-p filename
712 This function returns @code{t} if deleting the file @var{filename} and 763 This function returns @code{t} if deleting the file @var{filename} and
713 then creating it anew would keep the file's owner unchanged. 764 then creating it anew would keep the file's owner unchanged.
930 981
931 @c Wordy so as to prevent an overfull hbox. --rjc 15mar92 982 @c Wordy so as to prevent an overfull hbox. --rjc 15mar92
932 @item 983 @item
933 The number of names the file has. Alternate names, also known as hard 984 The number of names the file has. Alternate names, also known as hard
934 links, can be created by using the @code{add-name-to-file} function 985 links, can be created by using the @code{add-name-to-file} function
935 (@pxref{Changing File Attributes}). 986 (@pxref{Changing Files}).
936 987
937 @item 988 @item
938 The file's @sc{uid}. 989 The file's @sc{uid}.
939 990
940 @item 991 @item
1036 @item -32252 1087 @item -32252
1037 is on file system number -32252. 1088 is on file system number -32252.
1038 @end table 1089 @end table
1039 @end defun 1090 @end defun
1040 1091
1041 @node Changing File Attributes 1092 @node Changing Files
1042 @section Changing File Names and Attributes 1093 @section Changing File Names and Attributes
1043 @cindex renaming files 1094 @cindex renaming files
1044 @cindex copying files 1095 @cindex copying files
1045 @cindex deleting files 1096 @cindex deleting files
1046 @cindex linking files 1097 @cindex linking files
1088 the files again. This shows two names for one file, @file{foo} and 1139 the files again. This shows two names for one file, @file{foo} and
1089 @file{foo2}. 1140 @file{foo2}.
1090 1141
1091 @example 1142 @example
1092 @group 1143 @group
1093 (add-name-to-file "~/lewis/foo1" "~/lewis/foo2") 1144 (add-name-to-file "~/lewis/foo" "~/lewis/foo2")
1094 @result{} nil 1145 @result{} nil
1095 @end group 1146 @end group
1096 1147
1097 @group 1148 @group
1098 % ls -l fo* 1149 % ls -l fo*
1149 1200
1150 @deffn Command copy-file oldname newname &optional ok-if-exists time 1201 @deffn Command copy-file oldname newname &optional ok-if-exists time
1151 This command copies the file @var{oldname} to @var{newname}. An 1202 This command copies the file @var{oldname} to @var{newname}. An
1152 error is signaled if @var{oldname} does not exist. 1203 error is signaled if @var{oldname} does not exist.
1153 1204
1154 If @var{time} is non-@code{nil}, then this functions gives the new 1205 If @var{time} is non-@code{nil}, then this functions gives the new file
1155 file the same last-modified time that the old one has. (This works on 1206 the same last-modified time that the old one has. (This works on only
1156 only some operating systems.) 1207 some operating systems.) If setting the time gets an error,
1208 @code{copy-file} signals a @code{file-date-error} error.
1157 1209
1158 In an interactive call, this function prompts for @var{filename} and 1210 In an interactive call, this function prompts for @var{filename} and
1159 @var{newname} in the minibuffer; also, it requests confirmation if 1211 @var{newname} in the minibuffer; also, it requests confirmation if
1160 @var{newname} already exists. 1212 @var{newname} already exists.
1161 @end deffn 1213 @end deffn
1200 This function sets the default file protection for new files created by 1252 This function sets the default file protection for new files created by
1201 Emacs and its subprocesses. Every file created with Emacs initially has 1253 Emacs and its subprocesses. Every file created with Emacs initially has
1202 this protection. On Unix, the default protection is the bitwise 1254 this protection. On Unix, the default protection is the bitwise
1203 complement of the ``umask'' value. 1255 complement of the ``umask'' value.
1204 1256
1205 The argument @var{mode} must be an integer. Only the low 9 bits of 1257 The argument @var{mode} must be an integer. On most systems, only the
1206 @var{mode} are used. 1258 low 9 bits of @var{mode} are meaningful.
1207 1259
1208 Saving a modified version of an existing file does not count as creating 1260 Saving a modified version of an existing file does not count as creating
1209 the file; it does not change the file's mode, and does not use the 1261 the file; it does not change the file's mode, and does not use the
1210 default file protection. 1262 default file protection.
1211 @end defun 1263 @end defun
1228 Files are generally referred to by their names, in Emacs as elsewhere. 1280 Files are generally referred to by their names, in Emacs as elsewhere.
1229 File names in Emacs are represented as strings. The functions that 1281 File names in Emacs are represented as strings. The functions that
1230 operate on a file all expect a file name argument. 1282 operate on a file all expect a file name argument.
1231 1283
1232 In addition to operating on files themselves, Emacs Lisp programs 1284 In addition to operating on files themselves, Emacs Lisp programs
1233 often need to operate on the names; i.e., to take them apart and to use 1285 often need to operate on file names; i.e., to take them apart and to use
1234 part of a name to construct related file names. This section describes 1286 part of a name to construct related file names. This section describes
1235 how to manipulate file names. 1287 how to manipulate file names.
1236 1288
1237 The functions in this section do not actually access files, so they 1289 The functions in this section do not actually access files, so they
1238 can operate on file names that do not refer to an existing file or 1290 can operate on file names that do not refer to an existing file or
1239 directory. 1291 directory.
1240 1292
1241 On VMS, all these functions understand both VMS file-name syntax and 1293 On VMS, all these functions understand both VMS file-name syntax and
1242 Unix syntax. This is so that all the standard Lisp libraries can 1294 Unix syntax. This is so that all the standard Lisp libraries can
1243 specify file names in Unix syntax and work properly on VMS without 1295 specify file names in Unix syntax and work properly on VMS without
1244 change. On MS-DOS, these functions understand MS-DOS file-name syntax 1296 change. On MS-DOS and MS-Windows, these functions understand MS-DOS or
1245 as well as Unix syntax. 1297 MS-Windows file-name syntax as well as Unix syntax.
1246 1298
1247 @menu 1299 @menu
1248 * File Name Components:: The directory part of a file name, and the rest. 1300 * File Name Components:: The directory part of a file name, and the rest.
1249 * Directory Names:: A directory's name as a directory 1301 * Directory Names:: A directory's name as a directory
1250 is different from its name as a file. 1302 is different from its name as a file.
1273 slash; the nondirectory part is the rest. The rules in VMS syntax are 1325 slash; the nondirectory part is the rest. The rules in VMS syntax are
1274 complicated. 1326 complicated.
1275 1327
1276 For some purposes, the nondirectory part is further subdivided into 1328 For some purposes, the nondirectory part is further subdivided into
1277 the name proper and the @dfn{version number}. On Unix, only backup 1329 the name proper and the @dfn{version number}. On Unix, only backup
1278 files have version numbers in their names; on VMS, every file has a 1330 files have version numbers in their names. On VMS, every file has a
1279 version number, but most of the time the file name actually used in 1331 version number, but most of the time the file name actually used in
1280 Emacs omits the version number. Version numbers are found mostly in 1332 Emacs omits the version number, so that version numbers in Emacs are
1281 directory lists. 1333 found mostly in directory lists.
1282 1334
1283 @defun file-name-directory filename 1335 @defun file-name-directory filename
1284 This function returns the directory part of @var{filename} (or 1336 This function returns the directory part of @var{filename} (or
1285 @code{nil} if @var{filename} does not include a directory part). On 1337 @code{nil} if @var{filename} does not include a directory part). On
1286 Unix, the function returns a string ending in a slash. On VMS, it 1338 Unix, the function returns a string ending in a slash. On VMS, it
1287 returns a string ending in one of the three characters @samp{:}, 1339 returns a string ending in one of the three characters @samp{:},
1288 @samp{]}, or @samp{>}. 1340 @samp{]}, or @samp{>}.
1289 1341
1302 @end group 1354 @end group
1303 @end example 1355 @end example
1304 @end defun 1356 @end defun
1305 1357
1306 @defun file-name-nondirectory filename 1358 @defun file-name-nondirectory filename
1307 This function returns the nondirectory part of @var{filename}. 1359 This function returns the nondirectory part of @var{filename}.
1308 1360
1309 @example 1361 @example
1310 @group 1362 @group
1311 (file-name-nondirectory "lewis/foo") 1363 (file-name-nondirectory "lewis/foo")
1312 @result{} "foo" 1364 @result{} "foo"
1322 @end group 1374 @end group
1323 @end example 1375 @end example
1324 @end defun 1376 @end defun
1325 1377
1326 @defun file-name-sans-versions filename 1378 @defun file-name-sans-versions filename
1327 This function returns @var{filename} without any file version numbers, 1379 This function returns @var{filename} with any file version numbers,
1328 backup version numbers, or trailing tildes. 1380 backup version numbers, or trailing tildes deleted.
1329 1381
1330 @example 1382 @example
1331 @group 1383 @group
1332 (file-name-sans-versions "~rms/foo.~1~") 1384 (file-name-sans-versions "~rms/foo.~1~")
1333 @result{} "~rms/foo" 1385 @result{} "~rms/foo"
1385 such as @samp{$HOME}, and the constructs @samp{~}, and @samp{..}. 1437 such as @samp{$HOME}, and the constructs @samp{~}, and @samp{..}.
1386 1438
1387 @defun file-name-as-directory filename 1439 @defun file-name-as-directory filename
1388 This function returns a string representing @var{filename} in a form 1440 This function returns a string representing @var{filename} in a form
1389 that the operating system will interpret as the name of a directory. In 1441 that the operating system will interpret as the name of a directory. In
1390 Unix, this means appending a slash to the string. On VMS, the function 1442 Unix, this means appending a slash to the string (if it does not already
1391 converts a string of the form @file{[X]Y.DIR.1} to the form 1443 end in one). On VMS, the function converts a string of the form
1392 @file{[X.Y]}. 1444 @file{[X]Y.DIR.1} to the form @file{[X.Y]}.
1393 1445
1394 @example 1446 @example
1395 @group 1447 @group
1396 (file-name-as-directory "~rms/lewis") 1448 (file-name-as-directory "~rms/lewis")
1397 @result{} "~rms/lewis/" 1449 @result{} "~rms/lewis/"
1398 @end group 1450 @end group
1399 @end example 1451 @end example
1400 @end defun 1452 @end defun
1401 1453
1402 @defun directory-file-name dirname 1454 @defun directory-file-name dirname
1403 This function returns a string representing @var{dirname} in a form 1455 This function returns a string representing @var{dirname} in a form that
1404 that the operating system will interpret as the name of a file. On 1456 the operating system will interpret as the name of a file. On Unix,
1405 Unix, this means removing a final slash from the string. On VMS, the 1457 this means removing the final slash from the string. On VMS, the
1406 function converts a string of the form @file{[X.Y]} to 1458 function converts a string of the form @file{[X.Y]} to
1407 @file{[X]Y.DIR.1}. 1459 @file{[X]Y.DIR.1}.
1408 1460
1409 @example 1461 @example
1410 @group 1462 @group
1498 be expanded. Expansion also simplifies file names by eliminating 1550 be expanded. Expansion also simplifies file names by eliminating
1499 redundancies such as @file{./} and @file{@var{name}/../}. 1551 redundancies such as @file{./} and @file{@var{name}/../}.
1500 1552
1501 @defun expand-file-name filename &optional directory 1553 @defun expand-file-name filename &optional directory
1502 This function converts @var{filename} to an absolute file name. If 1554 This function converts @var{filename} to an absolute file name. If
1503 @var{directory} is supplied, it is the directory to start with if 1555 @var{directory} is supplied, it is the default directory to start with
1504 @var{filename} is relative. (The value of @var{directory} should itself 1556 if @var{filename} is relative. (The value of @var{directory} should
1505 be an absolute directory name; it may start with @samp{~}.) 1557 itself be an absolute directory name; it may start with @samp{~}.)
1506 Otherwise, the current buffer's value of @code{default-directory} is 1558 Otherwise, the current buffer's value of @code{default-directory} is
1507 used. For example: 1559 used. For example:
1508 1560
1509 @example 1561 @example
1510 @group 1562 @group
1532 @group 1584 @group
1533 (expand-file-name "bar/../foo") 1585 (expand-file-name "bar/../foo")
1534 @result{} "/xcssun/users/rms/lewis/foo" 1586 @result{} "/xcssun/users/rms/lewis/foo"
1535 @end group 1587 @end group
1536 @end example 1588 @end example
1537
1538 @samp{~/} is expanded into the user's home directory. A @samp{/} or
1539 @samp{~} following a @samp{/} is taken to be the start of an absolute
1540 file name that overrides what precedes it, so everything before that
1541 @samp{/} or @samp{~} is deleted. For example:
1542
1543 @example
1544 @group
1545 (expand-file-name
1546 "/a1/gnu//usr/local/lib/emacs/etc/MACHINES")
1547 @result{} "/usr/local/lib/emacs/etc/MACHINES"
1548 @end group
1549 @group
1550 (expand-file-name "/a1/gnu/~/foo")
1551 @result{} "/xcssun/users/rms/foo"
1552 @end group
1553 @end example
1554
1555 @noindent
1556 In both cases, @file{/a1/gnu/} is discarded because an absolute file
1557 name follows it.
1558 1589
1559 Note that @code{expand-file-name} does @emph{not} expand environment 1590 Note that @code{expand-file-name} does @emph{not} expand environment
1560 variables; only @code{substitute-in-file-name} does that. 1591 variables; only @code{substitute-in-file-name} does that.
1561 @end defun 1592 @end defun
1562 1593
1629 @result{} "~/foo" 1660 @result{} "~/foo"
1630 @end group 1661 @end group
1631 @group 1662 @group
1632 (substitute-in-file-name "/usr/local/$HOME/foo") 1663 (substitute-in-file-name "/usr/local/$HOME/foo")
1633 @result{} "/xcssun/users/rms/foo" 1664 @result{} "/xcssun/users/rms/foo"
1665 ;; @r{@file{/usr/local/} has been discarded.}
1634 @end group 1666 @end group
1635 @end example 1667 @end example
1636 1668
1637 On VMS, @samp{$} substitution is not done, so this function does nothing 1669 On VMS, @samp{$} substitution is not done, so this function does nothing
1638 on VMS except discard superfluous initial components as shown above. 1670 on VMS except discard superfluous initial components as shown above.
1643 1675
1644 Some programs need to write temporary files. Here is the usual way to 1676 Some programs need to write temporary files. Here is the usual way to
1645 construct a name for such a file: 1677 construct a name for such a file:
1646 1678
1647 @example 1679 @example
1648 (make-temp-name (concat "/tmp/" @var{name-of-application})) 1680 (make-temp-name
1649 @end example 1681 (expand-file-name @var{name-of-application}
1650 1682 (or (getenv "TMPDIR")
1683 "/tmp/")))
1684 @end example
1685
1686 @cindex @code{TMPDIR} environment variable.
1651 @noindent 1687 @noindent
1652 Here we use the directory @file{/tmp/} because that is the standard 1688 The job of @code{make-temp-name} is to prevent two different users or
1653 place on Unix for temporary files. The job of @code{make-temp-name} is 1689 two different jobs from trying to use the same name.
1654 to prevent two different users or two different jobs from trying to use 1690
1655 the same name. 1691 This example uses the environment variable @code{TMPDIR} to specify the
1692 directory, and if that is not specified, we use the directory
1693 @file{/tmp/}. This is the standard way to choose the directory, and all
1694 Emacs Lisp programs should use it.
1656 1695
1657 @defun make-temp-name string 1696 @defun make-temp-name string
1658 This function generates string that can be used as a unique name. The 1697 This function generates string that can be used as a unique name. The
1659 name starts with @var{string}, and ends with a number that is different 1698 name starts with @var{string}, and ends with a number that is different
1660 in each Emacs job. 1699 in each Emacs job.
1868 The argument @var{file} may be either a directory name or a file 1907 The argument @var{file} may be either a directory name or a file
1869 specification including wildcard characters. If @var{wildcard} is 1908 specification including wildcard characters. If @var{wildcard} is
1870 non-@code{nil}, that means treat @var{file} as a file specification with 1909 non-@code{nil}, that means treat @var{file} as a file specification with
1871 wildcards. 1910 wildcards.
1872 1911
1873 If @var{full-directory-p} is non-@code{nil}, that means @var{file} is a 1912 If @var{full-directory-p} is non-@code{nil}, that the directory listing
1874 directory and switches do not contain @samp{-d}, so that the listing 1913 is expected to show a complete directory. You should specify @code{t}
1875 should show the full contents of the directory. (The @samp{-d} option 1914 when @var{file} is a directory and switches do not contain @samp{-d}.
1876 to @code{ls} says to describe a directory itself rather than its 1915 (The @samp{-d} option to @code{ls} says to describe a directory itself
1877 contents.) 1916 as a file, rather than showing its contents.)
1878 1917
1879 This function works by running a directory listing program whose name is 1918 This function works by running a directory listing program whose name is
1880 in the variable @code{insert-directory-program}. If @var{wildcard} is 1919 in the variable @code{insert-directory-program}. If @var{wildcard} is
1881 non-@code{nil}, it also runs the shell specified by 1920 non-@code{nil}, it also runs the shell specified by
1882 @code{shell-file-name}, to expand the wildcards. 1921 @code{shell-file-name}, to expand the wildcards.
1910 @node Magic File Names 1949 @node Magic File Names
1911 @section Making Certain File Names ``Magic'' 1950 @section Making Certain File Names ``Magic''
1912 @cindex magic file names 1951 @cindex magic file names
1913 1952
1914 @c Emacs 19 feature 1953 @c Emacs 19 feature
1915 You can implement special handling for certain file names. This is 1954 You can implement special handling for certain file names. This is
1916 called making those names @dfn{magic}. You must supply a regular 1955 called making those names @dfn{magic}. The principal use for this
1956 feature is in implementing remote file names (@pxref{Remote Files,,
1957 Remote Files, emacs, The GNU Emacs Manual}).
1958
1959 To define a kind of magic file name, you must supply a regular
1917 expression to define the class of names (all those that match the 1960 expression to define the class of names (all those that match the
1918 regular expression), plus a handler that implements all the primitive 1961 regular expression), plus a handler that implements all the primitive
1919 Emacs file operations for file names that do match. 1962 Emacs file operations for file names that do match.
1920 1963
1921 The variable @code{file-name-handler-alist} holds a list of handlers, 1964 The variable @code{file-name-handler-alist} holds a list of handlers,
1922 together with regular expressions that determine when to apply each 1965 together with regular expressions that determine when to apply each
1923 handler. Each element has this form: 1966 handler. Each element has this form:
1924 1967
1925 @example 1968 @example
1926 (@var{regexp} . @var{handler}) 1969 (@var{regexp} . @var{handler})
2001 (cond ((eq operation 'insert-file-contents) @dots{}) 2044 (cond ((eq operation 'insert-file-contents) @dots{})
2002 ((eq operation 'write-region) @dots{}) 2045 ((eq operation 'write-region) @dots{})
2003 @dots{} 2046 @dots{}
2004 ;; @r{Handle any operation we don't know about.} 2047 ;; @r{Handle any operation we don't know about.}
2005 (t (let ((inhibit-file-name-handlers 2048 (t (let ((inhibit-file-name-handlers
2006 (cons 'my-file-handler 2049 (cons 'my-file-handler
2007 (and (eq inhibit-file-name-operation operation) 2050 (and (eq inhibit-file-name-operation operation)
2008 inhibit-file-name-handlers))) 2051 inhibit-file-name-handlers)))
2009 (inhibit-file-name-operation operation)) 2052 (inhibit-file-name-operation operation))
2010 (apply operation args))))) 2053 (apply operation args)))))
2011 @end smallexample 2054 @end smallexample
2012 2055
2013 When a handler function decides to call the ordinary Emacs primitive for 2056 When a handler function decides to call the ordinary Emacs primitive for
2014 the operation at hand, it needs to prevent the primitive from calling 2057 the operation at hand, it needs to prevent the primitive from calling
2048 If @var{filename} is an ordinary file name, not magic, then this function 2091 If @var{filename} is an ordinary file name, not magic, then this function
2049 does nothing and returns @code{nil}. 2092 does nothing and returns @code{nil}.
2050 @end defun 2093 @end defun
2051 2094
2052 @defun unhandled-file-name-directory filename 2095 @defun unhandled-file-name-directory filename
2053 This function returns the name of a directory that is not magic. 2096 This function returns the name of a directory that is not magic. It
2054 It uses the directory part of @var{filename} if that is not magic. 2097 uses the directory part of @var{filename} if that is not magic. For a
2055 Otherwise, it asks the handler what to do. 2098 magic file name, it invokes the file name handler, which therefore
2099 decides what value to return.
2056 2100
2057 This is useful for running a subprocess; every subprocess must have a 2101 This is useful for running a subprocess; every subprocess must have a
2058 non-magic directory to serve as its current directory, and this function 2102 non-magic directory to serve as its current directory, and this function
2059 is a good way to come up with one. 2103 is a good way to come up with one.
2060 @end defun 2104 @end defun
2094 @item regexp 2138 @item regexp
2095 A regular expression which is used to recognize files represented in 2139 A regular expression which is used to recognize files represented in
2096 this format. 2140 this format.
2097 2141
2098 @item from-fn 2142 @item from-fn
2099 A function to call to decode data in this format (to convert file data into 2143 A function or shell command to decode data in this format (to convert
2100 the usual Emacs data representation). 2144 file data into the usual Emacs data representation).
2101 2145
2102 The @var{from-fn} is called with two args, @var{begin} and @var{end}, 2146 If @var{from-fn} is a function, it is called with two args, @var{begin}
2103 which specify the part of the buffer it should convert. It should convert 2147 and @var{end}, which specify the part of the buffer it should convert.
2104 the text by editing it in place. Since this can change the length of the 2148 It should convert the text by editing it in place. Since this can
2105 text, @var{from-fn} should return the modified end position. 2149 change the length of the text, @var{from-fn} should return the modified
2150 end position.
2106 2151
2107 One responsibility of @var{from-fn} is to make sure that the beginning 2152 One responsibility of @var{from-fn} is to make sure that the beginning
2108 of the file no longer matches @var{regexp}. Otherwise it is likely to 2153 of the file no longer matches @var{regexp}. Otherwise it is likely to
2109 get called again. 2154 get called again.
2110 2155
2156 If @var{from-fn} is a string, it is a shell command; Emacs runs the
2157 command as a filter to perform the conversion.
2158
2111 @item to-fn 2159 @item to-fn
2112 A function to call to encode data in this format (to convert 2160 A function or shell command to encode data in this format (to convert
2113 the usual Emacs data representation into this format). 2161 the usual Emacs data representation into this format).
2114 2162
2115 The @var{to-fn} is called with two args, @var{begin} and @var{end}, 2163 If @var{to-fn} is a string, it is a shell command; Emacs runs the
2116 which specify the part of the buffer it should convert. There are 2164 command as a filter to perform the conversion.
2117 two ways it can do the conversion: 2165
2166 If @var{to-fn} is a function, it is called with two args, @var{begin}
2167 and @var{end}, which specify the part of the buffer it should convert.
2168 There are two ways it can do the conversion:
2118 2169
2119 @itemize @bullet 2170 @itemize @bullet
2120 @item 2171 @item
2121 By editing the buffer in place. In this case, @var{to-fn} should 2172 By editing the buffer in place. In this case, @var{to-fn} should
2122 return the end-position of the range of text, as modified. 2173 return the end-position of the range of text, as modified.
2164 2215
2165 When @code{write-region} writes data into a file, it first calls the 2216 When @code{write-region} writes data into a file, it first calls the
2166 encoding functions for the formats listed in @code{buffer-file-format}, 2217 encoding functions for the formats listed in @code{buffer-file-format},
2167 in the order of appearance in the list. 2218 in the order of appearance in the list.
2168 2219
2169 @defun format-write-file file format 2220 @deffn Command format-write-file file format
2170 This command writes the current buffer contents into the file @var{file} 2221 This command writes the current buffer contents into the file @var{file}
2171 in format @var{format}, and makes that format the default for future 2222 in format @var{format}, and makes that format the default for future
2172 saves of the buffer. The argument @var{format} is a list of format 2223 saves of the buffer. The argument @var{format} is a list of format
2173 names. 2224 names.
2174 @end defun 2225 @end deffn
2175 2226
2176 @defun format-find-file file format 2227 @deffn Command format-find-file file format
2177 This command finds the file @var{file}, converting it according to 2228 This command finds the file @var{file}, converting it according to
2178 format @var{format}. It also makes @var{format} the default if the 2229 format @var{format}. It also makes @var{format} the default if the
2179 buffer is saved later. 2230 buffer is saved later.
2180 2231
2181 The argument @var{format} is a list of format names. If @var{format} is 2232 The argument @var{format} is a list of format names. If @var{format} is
2182 @code{nil}, no conversion takes place. Interactively, typing just 2233 @code{nil}, no conversion takes place. Interactively, typing just
2183 @key{RET} for @var{format} specifies @code{nil}. 2234 @key{RET} for @var{format} specifies @code{nil}.
2184 @end defun 2235 @end deffn
2185 2236
2186 @defun format-insert-file file format %optional beg end 2237 @deffn format-insert-file file format %optional beg end
2187 This command inserts the contents of file @var{file}, converting it 2238 This command inserts the contents of file @var{file}, converting it
2188 according to format @var{format}. If @var{beg} and @var{end} are 2239 according to format @var{format}. If @var{beg} and @var{end} are
2189 non-@code{nil}, they specify which part of the file to read, as in 2240 non-@code{nil}, they specify which part of the file to read, as in
2190 @code{insert-file-contents} (@pxref{Reading from Files}). 2241 @code{insert-file-contents} (@pxref{Reading from Files}).
2191 2242
2194 (after conversion). 2245 (after conversion).
2195 2246
2196 The argument @var{format} is a list of format names. If @var{format} is 2247 The argument @var{format} is a list of format names. If @var{format} is
2197 @code{nil}, no conversion takes place. Interactively, typing just 2248 @code{nil}, no conversion takes place. Interactively, typing just
2198 @key{RET} for @var{format} specifies @code{nil}. 2249 @key{RET} for @var{format} specifies @code{nil}.
2199 @end defun 2250 @end deffn
2200 2251
2201 @defvar auto-save-file-format 2252 @defvar auto-save-file-format
2202 This variable specifies the format to use for auto-saving. Its value is 2253 This variable specifies the format to use for auto-saving. Its value is
2203 a list of format names, just like the value of 2254 a list of format names, just like the value of
2204 @code{buffer-file-format}; but it is used instead of 2255 @code{buffer-file-format}; but it is used instead of
2211 @cindex MS-DOS file types 2262 @cindex MS-DOS file types
2212 @cindex file types on MS-DOS 2263 @cindex file types on MS-DOS
2213 @cindex text files and binary files 2264 @cindex text files and binary files
2214 @cindex binary files and text files 2265 @cindex binary files and text files
2215 @cindex Windows file types 2266 @cindex Windows file types
2267
2268 @c ??? This needs to be updated.
2216 2269
2217 Emacs on MS-DOS and on Windows NT or 95 makes a distinction between 2270 Emacs on MS-DOS and on Windows NT or 95 makes a distinction between
2218 text files and binary files. This is necessary because ordinary text 2271 text files and binary files. This is necessary because ordinary text
2219 files on MS-DOS use a two character sequence between lines: 2272 files on MS-DOS use a two character sequence between lines:
2220 carriage-return and linefeed (@sc{crlf}). Emacs expects just a newline 2273 carriage-return and linefeed (@sc{crlf}). Emacs expects just a newline
2238 @end defun 2291 @end defun
2239 2292
2240 @defopt file-name-buffer-file-type-alist 2293 @defopt file-name-buffer-file-type-alist
2241 This variable holds an alist for distinguishing text files from binary 2294 This variable holds an alist for distinguishing text files from binary
2242 files. Each element has the form (@var{regexp} . @var{type}), where 2295 files. Each element has the form (@var{regexp} . @var{type}), where
2243 @var{regexp} is matched against the file name, and @var{type} may be is 2296 @var{regexp} is matched against the file name, and @var{type} may be
2244 @code{nil} for text, @code{t} for binary, or a function to call to 2297 @code{nil} for text, @code{t} for binary, or a function to call to
2245 compute which. If it is a function, then it is called with a single 2298 compute which. If it is a function, then it is called with a single
2246 argument (the file name) and should return @code{t} or @code{nil}. 2299 argument (the file name) and should return @code{t} or @code{nil}.
2247 @end defopt 2300 @end defopt
2248 2301