Mercurial > emacs
comparison lispref/files.texi @ 22138:d4ac295a98b3
*** empty log message ***
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Tue, 19 May 1998 03:45:57 +0000 |
parents | 90da2489c498 |
children | 71f954d59214 |
comparison
equal
deleted
inserted
replaced
22137:2b0e6a1e7fb9 | 22138:d4ac295a98b3 |
---|---|
13 @ref{Buffers}, and those related to backups and auto-saving are | 13 @ref{Buffers}, and those related to backups and auto-saving are |
14 described in @ref{Backups and Auto-Saving}. | 14 described in @ref{Backups and Auto-Saving}. |
15 | 15 |
16 Many of the file functions take one or more arguments that are file | 16 Many of the file functions take one or more arguments that are file |
17 names. A file name is actually a string. Most of these functions | 17 names. A file name is actually a string. Most of these functions |
18 expand file name arguments using @code{expand-file-name}, so that | 18 expand file name arguments by calling @code{expand-file-name}, so that |
19 @file{~} is handled correctly, as are relative file names (including | 19 @file{~} is handled correctly, as are relative file names (including |
20 @samp{../}). These functions don't recognize environment variable | 20 @samp{../}). These functions don't recognize environment variable |
21 substitutions such as @samp{$HOME}. @xref{File Name Expansion}. | 21 substitutions such as @samp{$HOME}. @xref{File Name Expansion}. |
22 | 22 |
23 @menu | 23 @menu |
199 | 199 |
200 @node Subroutines of Visiting | 200 @node Subroutines of Visiting |
201 @comment node-name, next, previous, up | 201 @comment node-name, next, previous, up |
202 @subsection Subroutines of Visiting | 202 @subsection Subroutines of Visiting |
203 | 203 |
204 The @code{find-file-noselect} function uses the | 204 The @code{find-file-noselect} function uses two important subroutines |
205 @code{create-file-buffer} and @code{after-find-file} functions as | 205 which are sometimes useful in user Lisp code: @code{create-file-buffer} |
206 subroutines. Sometimes it is useful to call them directly. | 206 and @code{after-find-file}. This section explains how to use them. |
207 | 207 |
208 @defun create-file-buffer filename | 208 @defun create-file-buffer filename |
209 This function creates a suitably named buffer for visiting | 209 This function creates a suitably named buffer for visiting |
210 @var{filename}, and returns it. It uses @var{filename} (sans directory) | 210 @var{filename}, and returns it. It uses @var{filename} (sans directory) |
211 as the name if that name is free; otherwise, it appends a string such as | 211 as the name if that name is free; otherwise, it appends a string such as |
249 | 249 |
250 If @var{warn} is non-@code{nil}, then this function issues a warning | 250 If @var{warn} is non-@code{nil}, then this function issues a warning |
251 if an auto-save file exists and is more recent than the visited file. | 251 if an auto-save file exists and is more recent than the visited file. |
252 | 252 |
253 The last thing @code{after-find-file} does is call all the functions | 253 The last thing @code{after-find-file} does is call all the functions |
254 in @code{find-file-hooks}. | 254 in the list @code{find-file-hooks}. |
255 @end defun | 255 @end defun |
256 | 256 |
257 @node Saving Buffers | 257 @node Saving Buffers |
258 @section Saving Buffers | 258 @section Saving Buffers |
259 | 259 |
332 | 332 |
333 You might wish to save the file modes value returned by | 333 You might wish to save the file modes value returned by |
334 @code{backup-buffer} and use that to set the mode bits of the file that | 334 @code{backup-buffer} and use that to set the mode bits of the file that |
335 you write. This is what @code{save-buffer} normally does. | 335 you write. This is what @code{save-buffer} normally does. |
336 | 336 |
337 The hook functions in @code{write-file-hooks} are also responsible for | |
338 encoding the data (if desired): they must choose a suitable coding | |
339 system (@pxref{Lisp and Coding Systems}), perform the encoding | |
340 (@pxref{Explicit Encoding}), and set @code{last-coding-system-used} to | |
341 the coding system that was used (@pxref{Specifying Coding Systems}). | |
342 | |
337 Do not make this variable buffer-local. To set up buffer-specific hook | 343 Do not make this variable buffer-local. To set up buffer-specific hook |
338 functions, use @code{write-contents-hooks} instead. | 344 functions, use @code{write-contents-hooks} instead. |
339 | 345 |
340 Even though this is not a normal hook, you can use @code{add-hook} and | 346 Even though this is not a normal hook, you can use @code{add-hook} and |
341 @code{remove-hook} to manipulate the list. @xref{Hooks}. | 347 @code{remove-hook} to manipulate the list. @xref{Hooks}. |
446 contents of the buffer (actually, just the accessible portion) with the | 452 contents of the buffer (actually, just the accessible portion) with the |
447 contents of the file. This is better than simply deleting the buffer | 453 contents of the file. This is better than simply deleting the buffer |
448 contents and inserting the whole file, because (1) it preserves some | 454 contents and inserting the whole file, because (1) it preserves some |
449 marker positions and (2) it puts less data in the undo list. | 455 marker positions and (2) it puts less data in the undo list. |
450 | 456 |
451 It works to read a special file with @code{insert-file-contents} | 457 It is possible to read a special file (such as a FIFO or an I/O device) |
452 as long as @var{replace} and @var{visit} are @code{nil}. | 458 with @code{insert-file-contents}, as long as @var{replace} and |
453 @end defun | 459 @var{visit} are @code{nil}. |
454 | 460 @end defun |
461 | |
462 @defun insert-file-contents-literally filename &optional visit beg end replace | |
455 @tindex insert-file-contents-literally | 463 @tindex insert-file-contents-literally |
456 @defun insert-file-contents-literally filename &optional visit beg end replace | |
457 This function works like @code{insert-file-contents} except that it does | 464 This function works like @code{insert-file-contents} except that it does |
458 not do format decoding (@pxref{Format Conversion}), does not do | 465 not do format decoding (@pxref{Format Conversion}), does not do |
459 character code conversion (@pxref{Coding Systems}), does not run | 466 character code conversion (@pxref{Coding Systems}), does not run |
460 @code{find-file-hooks}, does not perform automatic uncompression, and so | 467 @code{find-file-hooks}, does not perform automatic uncompression, and so |
461 on. | 468 on. |
483 | 490 |
484 An error is signaled if @var{filename} specifies a nonwritable file, | 491 An error is signaled if @var{filename} specifies a nonwritable file, |
485 or a nonexistent file in a directory where files cannot be created. | 492 or a nonexistent file in a directory where files cannot be created. |
486 @end deffn | 493 @end deffn |
487 | 494 |
488 @deffn Command write-region start end filename &optional append visit | 495 @deffn Command write-region start end filename &optional append visit confirm |
489 This function writes the region delimited by @var{start} and @var{end} | 496 This function writes the region delimited by @var{start} and @var{end} |
490 in the current buffer into the file specified by @var{filename}. | 497 in the current buffer into the file specified by @var{filename}. |
491 | 498 |
492 @c Emacs 19 feature | 499 @c Emacs 19 feature |
493 If @var{start} is a string, then @code{write-region} writes or appends | 500 If @var{start} is a string, then @code{write-region} writes or appends |
494 that string, rather than text from the buffer. | 501 that string, rather than text from the buffer. |
495 | 502 |
496 If @var{append} is non-@code{nil}, then the specified text is appended | 503 If @var{append} is non-@code{nil}, then the specified text is appended |
497 to the existing file contents (if any). | 504 to the existing file contents (if any). |
505 | |
506 If @var{confirm} is non-@code{nil}, then @code{write-region} asks | |
507 for confirmation if @var{filename} names an existing file. | |
498 | 508 |
499 If @var{visit} is @code{t}, then Emacs establishes an association | 509 If @var{visit} is @code{t}, then Emacs establishes an association |
500 between the buffer and the file: the buffer is then visiting that file. | 510 between the buffer and the file: the buffer is then visiting that file. |
501 It also sets the last file modification time for the current buffer to | 511 It also sets the last file modification time for the current buffer to |
502 @var{filename}'s modtime, and marks the buffer as not modified. This | 512 @var{filename}'s modtime, and marks the buffer as not modified. This |
522 nor @code{nil} nor a string, then this message is inhibited. This | 532 nor @code{nil} nor a string, then this message is inhibited. This |
523 feature is useful for programs that use files for internal purposes, | 533 feature is useful for programs that use files for internal purposes, |
524 files that the user does not need to know about. | 534 files that the user does not need to know about. |
525 @end deffn | 535 @end deffn |
526 | 536 |
537 @defmac with-temp-file file body... | |
527 @tindex with-temp-file | 538 @tindex with-temp-file |
528 @defmac with-temp-file file body... | |
529 The @code{with-temp-file} macro evaluates the @var{body} forms with a | 539 The @code{with-temp-file} macro evaluates the @var{body} forms with a |
530 temporary buffer as the current buffer; then, at the end, it writes the | 540 temporary buffer as the current buffer; then, at the end, it writes the |
531 buffer contents into file @var{file}. It kills the temporary buffer | 541 buffer contents into file @var{file}. It kills the temporary buffer |
532 when finished, restoring the buffer that was current before the | 542 when finished, restoring the buffer that was current before the |
533 @code{with-temp-file} form. Then it returns the value of the last form | 543 @code{with-temp-file} form. Then it returns the value of the last form |
730 @noindent | 740 @noindent |
731 we can deduce that any attempt to read a file in @file{/foo/} will | 741 we can deduce that any attempt to read a file in @file{/foo/} will |
732 give an error. | 742 give an error. |
733 @end defun | 743 @end defun |
734 | 744 |
745 @defun access-file filename string | |
735 @tindex access-file | 746 @tindex access-file |
736 @defun access-file filename string | |
737 This function opens file @var{filename} for reading, then closes it and | 747 This function opens file @var{filename} for reading, then closes it and |
738 returns @code{nil}. However, if the open fails, it signals an error | 748 returns @code{nil}. However, if the open fails, it signals an error |
739 using @var{string} as the error message text. | 749 using @var{string} as the error message text. |
740 @end defun | 750 @end defun |
741 | 751 |
1101 In the first part of the following example, we list two files, | 1111 In the first part of the following example, we list two files, |
1102 @file{foo} and @file{foo3}. | 1112 @file{foo} and @file{foo3}. |
1103 | 1113 |
1104 @example | 1114 @example |
1105 @group | 1115 @group |
1106 % ls -l fo* | 1116 % ls -li fo* |
1107 -rw-rw-rw- 1 rms 29 Aug 18 20:32 foo | 1117 81908 -rw-rw-rw- 1 rms 29 Aug 18 20:32 foo |
1108 -rw-rw-rw- 1 rms 24 Aug 18 20:31 foo3 | 1118 84302 -rw-rw-rw- 1 rms 24 Aug 18 20:31 foo3 |
1109 @end group | 1119 @end group |
1110 @end example | 1120 @end example |
1111 | 1121 |
1112 Now we create a hard link, by calling @code{add-name-to-file}, then list | 1122 Now we create a hard link, by calling @code{add-name-to-file}, then list |
1113 the files again. This shows two names for one file, @file{foo} and | 1123 the files again. This shows two names for one file, @file{foo} and |
1114 @file{foo2}. | 1124 @file{foo2}. |
1115 | 1125 |
1116 @example | 1126 @example |
1117 @group | 1127 @group |
1118 (add-name-to-file "~/lewis/foo" "~/lewis/foo2") | 1128 (add-name-to-file "foo" "foo2") |
1119 @result{} nil | 1129 @result{} nil |
1120 @end group | 1130 @end group |
1121 | 1131 |
1122 @group | 1132 @group |
1123 % ls -l fo* | 1133 % ls -li fo* |
1124 -rw-rw-rw- 2 rms 29 Aug 18 20:32 foo | 1134 81908 -rw-rw-rw- 2 rms 29 Aug 18 20:32 foo |
1125 -rw-rw-rw- 2 rms 29 Aug 18 20:32 foo2 | 1135 81908 -rw-rw-rw- 2 rms 29 Aug 18 20:32 foo2 |
1126 -rw-rw-rw- 1 rms 24 Aug 18 20:31 foo3 | 1136 84302 -rw-rw-rw- 1 rms 24 Aug 18 20:31 foo3 |
1127 @end group | 1137 @end group |
1128 @end example | 1138 @end example |
1129 | 1139 |
1130 @c !!! Check whether this set of examples is consistent. --rjc 15mar92 | 1140 Finally, we evaluate the following: |
1131 Finally, we evaluate the following: | 1141 |
1132 | 1142 @example |
1133 @example | 1143 (add-name-to-file "foo" "foo3" t) |
1134 (add-name-to-file "~/lewis/foo" "~/lewis/foo3" t) | |
1135 @end example | 1144 @end example |
1136 | 1145 |
1137 @noindent | 1146 @noindent |
1138 and list the files again. Now there are three names | 1147 and list the files again. Now there are three names |
1139 for one file: @file{foo}, @file{foo2}, and @file{foo3}. The old | 1148 for one file: @file{foo}, @file{foo2}, and @file{foo3}. The old |
1140 contents of @file{foo3} are lost. | 1149 contents of @file{foo3} are lost. |
1141 | 1150 |
1142 @example | 1151 @example |
1143 @group | 1152 @group |
1144 (add-name-to-file "~/lewis/foo1" "~/lewis/foo3") | 1153 (add-name-to-file "foo1" "foo3") |
1145 @result{} nil | 1154 @result{} nil |
1146 @end group | 1155 @end group |
1147 | 1156 |
1148 @group | 1157 @group |
1149 % ls -l fo* | 1158 % ls -li fo* |
1150 -rw-rw-rw- 3 rms 29 Aug 18 20:32 foo | 1159 81908 -rw-rw-rw- 3 rms 29 Aug 18 20:32 foo |
1151 -rw-rw-rw- 3 rms 29 Aug 18 20:32 foo2 | 1160 81908 -rw-rw-rw- 3 rms 29 Aug 18 20:32 foo2 |
1152 -rw-rw-rw- 3 rms 29 Aug 18 20:32 foo3 | 1161 81908 -rw-rw-rw- 3 rms 29 Aug 18 20:32 foo3 |
1153 @end group | 1162 @end group |
1154 @end example | 1163 @end example |
1155 | 1164 |
1156 This function is meaningless on VMS, where multiple names for one file | 1165 This function is meaningless on operating systems where multiple names |
1157 are not allowed. | 1166 for one file are not allowed. |
1158 | 1167 |
1159 See also @code{file-nlinks} in @ref{File Attributes}. | 1168 See also @code{file-nlinks} in @ref{File Attributes}. |
1160 @end defun | 1169 @end defun |
1161 | 1170 |
1162 @deffn Command rename-file filename newname &optional ok-if-already-exists | 1171 @deffn Command rename-file filename newname &optional ok-if-already-exists |
1163 This command renames the file @var{filename} as @var{newname}. | 1172 This command renames the file @var{filename} as @var{newname}. |
1164 | 1173 |
1174 | 1183 |
1175 @deffn Command copy-file oldname newname &optional ok-if-exists time | 1184 @deffn Command copy-file oldname newname &optional ok-if-exists time |
1176 This command copies the file @var{oldname} to @var{newname}. An | 1185 This command copies the file @var{oldname} to @var{newname}. An |
1177 error is signaled if @var{oldname} does not exist. | 1186 error is signaled if @var{oldname} does not exist. |
1178 | 1187 |
1179 If @var{time} is non-@code{nil}, then this functions gives the new file | 1188 If @var{time} is non-@code{nil}, then this function gives the new file |
1180 the same last-modified time that the old one has. (This works on only | 1189 the same last-modified time that the old one has. (This works on only |
1181 some operating systems.) If setting the time gets an error, | 1190 some operating systems.) If setting the time gets an error, |
1182 @code{copy-file} signals a @code{file-date-error} error. | 1191 @code{copy-file} signals a @code{file-date-error} error. |
1183 | 1192 |
1184 In an interactive call, this function prompts for @var{filename} and | 1193 In an interactive call, this function prompts for @var{filename} and |
1577 this case, @code{file-relative-name} returns @var{filename} in absolute | 1586 this case, @code{file-relative-name} returns @var{filename} in absolute |
1578 form. | 1587 form. |
1579 | 1588 |
1580 @example | 1589 @example |
1581 (file-relative-name "/foo/bar" "/foo/") | 1590 (file-relative-name "/foo/bar" "/foo/") |
1582 @result{} "bar") | 1591 @result{} "bar" |
1583 (file-relative-name "/foo/bar" "/hack/") | 1592 (file-relative-name "/foo/bar" "/hack/") |
1584 @result{} "/foo/bar") | 1593 @result{} "/foo/bar" |
1585 @end example | 1594 @end example |
1586 @end defun | 1595 @end defun |
1587 | 1596 |
1588 @defvar default-directory | 1597 @defvar default-directory |
1589 The value of this buffer-local variable is the default directory for the | 1598 The value of this buffer-local variable is the default directory for the |
1651 construct a name for such a file: | 1660 construct a name for such a file: |
1652 | 1661 |
1653 @example | 1662 @example |
1654 (make-temp-name | 1663 (make-temp-name |
1655 (expand-file-name @var{name-of-application} | 1664 (expand-file-name @var{name-of-application} |
1656 (or (getenv "TMPDIR") | 1665 temporary-file-directory)) |
1657 "/tmp/"))) | 1666 @end example |
1658 @end example | 1667 |
1659 | |
1660 @cindex @code{TMPDIR} environment variable. | |
1661 @noindent | 1668 @noindent |
1662 The job of @code{make-temp-name} is to prevent two different users or | 1669 The job of @code{make-temp-name} is to prevent two different users or |
1663 two different jobs from trying to use the same name. | 1670 two different jobs from trying to use the exact same file name. This |
1664 | 1671 example uses the variable @code{temporary-file-directory} to decide |
1665 This example uses the environment variable @code{TMPDIR} to specify the | 1672 where to put the temporary file. All Emacs Lisp programs should |
1666 directory, and if that is not specified, we use the directory | 1673 use @code{temporary-file-directory} for this purpose, to give the user |
1667 @file{/tmp/}. This is the standard way to choose the directory, and all | 1674 a uniform way to specify the directory for all temporary files. |
1668 Emacs Lisp programs should use it. | |
1669 | 1675 |
1670 @defun make-temp-name string | 1676 @defun make-temp-name string |
1671 This function generates string that can be used as a unique name. The | 1677 This function generates string that can be used as a unique file name. |
1672 name starts with @var{string}, and ends with a number that is different | 1678 The name starts with @var{string}, and contains a number that is |
1673 in each Emacs job. | 1679 different in each Emacs job. |
1674 | 1680 |
1675 @example | 1681 @example |
1676 @group | 1682 @group |
1677 (make-temp-name "/tmp/foo") | 1683 (make-temp-name "/tmp/foo") |
1678 @result{} "/tmp/foo021304" | 1684 @result{} "/tmp/foo232J6v" |
1679 @end group | 1685 @end group |
1680 @end example | 1686 @end example |
1681 | 1687 |
1682 To prevent conflicts among different libraries running in the same | 1688 To prevent conflicts among different libraries running in the same |
1683 Emacs, each Lisp program that uses @code{make-temp-name} should have its | 1689 Emacs, each Lisp program that uses @code{make-temp-name} should have its |
1684 own @var{string}. The number added to the end of the name distinguishes | 1690 own @var{string}. The number added to the end of @var{string} |
1685 between the same application running in different Emacs jobs. | 1691 distinguishes between the same application running in different Emacs |
1686 @end defun | 1692 jobs. Additional added characters permit a large number of distinct |
1693 names even in one Emacs job. | |
1694 @end defun | |
1695 | |
1696 @defvar temporary-file-directory | |
1697 @cindex @code{TMPDIR} environment variable. | |
1698 @cindex @code{TMP} environment variable. | |
1699 This variable specifies the directory name for creating temporary files. | |
1700 Its value should be a directory name (@pxref{Directory Names}), but it | |
1701 is good for Lisp programs to cope if the value is a file name instead. | |
1702 (Using the value as the second argument to @code{expand-file-name} is a | |
1703 good way to achieve that.) | |
1704 | |
1705 The default value is determined in a reasonable way for your operating | |
1706 system; on GNU and Unix systems it is based on the @code{TMP} and | |
1707 @code{TMPDIR} environment variables. | |
1708 | |
1709 Even if you do not use @code{make-temp-name} to choose the temporary | |
1710 file's name, you should still use this variable to decide which | |
1711 directory to put the file in. | |
1712 @end defvar | |
1687 | 1713 |
1688 @node File Name Completion | 1714 @node File Name Completion |
1689 @subsection File Name Completion | 1715 @subsection File Name Completion |
1690 @cindex file name completion subroutines | 1716 @cindex file name completion subroutines |
1691 @cindex completion, file name | 1717 @cindex completion, file name |
1811 "*The file name to save completions to.") | 1837 "*The file name to save completions to.") |
1812 @end example | 1838 @end example |
1813 | 1839 |
1814 On GNU and Unix systems, and on some other systems as well, | 1840 On GNU and Unix systems, and on some other systems as well, |
1815 @code{convert-standard-filename} returns its argument unchanged. On | 1841 @code{convert-standard-filename} returns its argument unchanged. On |
1816 some other systems, it alters the name to fit the systems's conventions. | 1842 some other systems, it alters the name to fit the system's conventions. |
1817 | 1843 |
1818 For example, on MS-DOS the alterations made by this function include | 1844 For example, on MS-DOS the alterations made by this function include |
1819 converting a leading @samp{.} to @samp{_}, converting a @samp{_} in the | 1845 converting a leading @samp{.} to @samp{_}, converting a @samp{_} in the |
1820 middle of the name to @samp{.} if there is no other @samp{.}, inserting | 1846 middle of the name to @samp{.} if there is no other @samp{.}, inserting |
1821 a @samp{.} after eight characters if there is none, and truncating to | 1847 a @samp{.} after eight characters if there is none, and truncating to |
1881 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 |
1882 specification including wildcard characters. If @var{wildcard} is | 1908 specification including wildcard characters. If @var{wildcard} is |
1883 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 |
1884 wildcards. | 1910 wildcards. |
1885 | 1911 |
1886 If @var{full-directory-p} is non-@code{nil}, that the directory listing | 1912 If @var{full-directory-p} is non-@code{nil}, that means the directory |
1887 is expected to show a complete directory. You should specify @code{t} | 1913 listing is expected to show the full contents of a directory. You |
1888 when @var{file} is a directory and switches do not contain @samp{-d}. | 1914 should specify @code{t} when @var{file} is a directory and switches do |
1889 (The @samp{-d} option to @code{ls} says to describe a directory itself | 1915 not contain @samp{-d}. (The @samp{-d} option to @code{ls} says to |
1890 as a file, rather than showing its contents.) | 1916 describe a directory itself as a file, rather than showing its |
1917 contents.) | |
1891 | 1918 |
1892 This function works by running a directory listing program whose name is | 1919 This function works by running a directory listing program whose name is |
1893 in the variable @code{insert-directory-program}. If @var{wildcard} is | 1920 in the variable @code{insert-directory-program}. If @var{wildcard} is |
1894 non-@code{nil}, it also runs the shell specified by | 1921 non-@code{nil}, it also runs the shell specified by |
1895 @code{shell-file-name}, to expand the wildcards. | 1922 @code{shell-file-name}, to expand the wildcards. |
2228 @end deffn | 2255 @end deffn |
2229 | 2256 |
2230 @defvar auto-save-file-format | 2257 @defvar auto-save-file-format |
2231 This variable specifies the format to use for auto-saving. Its value is | 2258 This variable specifies the format to use for auto-saving. Its value is |
2232 a list of format names, just like the value of | 2259 a list of format names, just like the value of |
2233 @code{buffer-file-format}; but it is used instead of | 2260 @code{buffer-file-format}; however, it is used instead of |
2234 @code{buffer-file-format} for writing auto-save files. This variable | 2261 @code{buffer-file-format} for writing auto-save files. This variable is |
2235 is always buffer-local in all buffers. | 2262 always buffer-local in all buffers. |
2236 @end defvar | 2263 @end defvar |