]> code.delx.au - gnu-emacs/blobdiff - lispref/files.texi
*** empty log message ***
[gnu-emacs] / lispref / files.texi
index e27ab8f81d2c1f1bd9e48f0a83390a9b3b987c67..cb28ac8607ba7306fd1d2474f1b268f4d1f7116d 100644 (file)
@@ -1,7 +1,7 @@
 @c -*-texinfo-*-
 @c This is part of the GNU Emacs Lisp Reference Manual.
 @c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998, 1999
-@c   Free Software Foundation, Inc. 
+@c   Free Software Foundation, Inc.
 @c See the file elisp.texi for copying conditions.
 @setfilename ../info/files
 @node Files, Backups and Auto-Saving, Documentation, Top
@@ -92,7 +92,7 @@ temporary buffer.  Visiting the file is not necessary and takes longer.
 
 @deffn Command find-file filename &optional wildcards
 This command selects a buffer visiting the file @var{filename},
-using an existing buffer if there is one, and otherwise creating a 
+using an existing buffer if there is one, and otherwise creating a
 new buffer and reading the file into it.  It also returns that buffer.
 
 The body of the @code{find-file} function is very simple and looks
@@ -141,17 +141,19 @@ The @code{find-file-noselect} function normally calls
 Visiting}).  That function sets the buffer major mode, parses local
 variables, warns the user if there exists an auto-save file more recent
 than the file just visited, and finishes by running the functions in
-@code{find-file-hooks}.
+@code{find-file-hook}.
 
 If the optional argument @var{rawfile} is non-@code{nil}, then
 @code{after-find-file} is not called, and the
-@code{find-file-not-found-hooks} are not run in case of failure.  What's
+@code{find-file-not-found-functions} are not run in case of failure.  What's
 more, a non-@code{nil} @var{rawfile} value suppresses coding system
 conversion (@pxref{Coding Systems}) and format conversion (@pxref{Format
 Conversion}).
 
-The @code{find-file-noselect} function returns the buffer that is
-visiting the file @var{filename}.
+The @code{find-file-noselect} function usually returns the buffer that
+is visiting the file @var{filename}.  But, if wildcards are actually
+used and expanded, it returns a list of buffers that are visiting the
+various files.
 
 @example
 @group
@@ -198,7 +200,7 @@ match them.  If this is @code{nil}, then wildcard characters are
 not treated specially.
 @end defvar
 
-@defvar find-file-hooks
+@defvar find-file-hook
 The value of this variable is a list of functions to be called after a
 file is visited.  The file's local-variables specification (if any) will
 have been processed before the hooks are run.  The buffer visiting the
@@ -208,7 +210,7 @@ This variable works just like a normal hook, but we think that renaming
 it would not be advisable.  @xref{Hooks}.
 @end defvar
 
-@defvar find-file-not-found-hooks
+@defvar find-file-not-found-functions
 The value of this variable is a list of functions to be called when
 @code{find-file} or @code{find-file-noselect} is passed a nonexistent
 file name.  @code{find-file-noselect} calls these functions as soon as
@@ -283,11 +285,11 @@ of this variable.
 
 If @var{nomodes} is non-@code{nil}, that means don't alter the buffer's
 major mode, don't process local variables specifications in the file,
-and don't run @code{find-file-hooks}.  This feature is used by
+and don't run @code{find-file-hook}.  This feature is used by
 @code{revert-buffer} in some cases.
 
 The last thing @code{after-find-file} does is call all the functions
-in the list @code{find-file-hooks}.
+in the list @code{find-file-hook}.
 @end defun
 
 @node Saving Buffers
@@ -360,14 +362,14 @@ before overwriting an existing file.
 conversion (@pxref{Format Conversion}), and may save text properties in
 ``annotations'' (@pxref{Saving Properties}).
 
-@defvar write-file-hooks
+@defvar write-file-functions
 The value of this variable is a list of functions to be called before
 writing out a buffer to its visited file.  If one of them returns
 non-@code{nil}, the file is considered already written and the rest of
 the functions are not called, nor is the usual code for writing the file
 executed.
 
-If a function in @code{write-file-hooks} returns non-@code{nil}, it
+If a function in @code{write-file-functions} returns non-@code{nil}, it
 is responsible for making a backup file (if that is appropriate).
 To do so, execute the following code:
 
@@ -379,43 +381,33 @@ You might wish to save the file modes value returned by
 @code{backup-buffer} and use that to set the mode bits of the file that
 you write.  This is what @code{save-buffer} normally does.
 
-The hook functions in @code{write-file-hooks} are also responsible for
+The hook functions in @code{write-file-functions} are also responsible for
 encoding the data (if desired): they must choose a suitable coding
 system (@pxref{Lisp and Coding Systems}), perform the encoding
 (@pxref{Explicit Encoding}), and set @code{last-coding-system-used} to
 the coding system that was used (@pxref{Encoding and I/O}).
 
-Do not make this variable buffer-local.  To set up buffer-specific hook
-functions, use @code{write-contents-hooks} instead.
+If you set this hook locally in a buffer, it is assumed to be
+associated with the file or the way the contents of the buffer were
+obtained.  Thus the variable is marked as a permanent local, so that
+changing the major mode does not alter a buffer-local value.  On the
+other hand, calling @code{set-visited-file-name} will reset it.
+If this is not what you want, you might like to use
+@code{write-contents-functions} instead.
 
 Even though this is not a normal hook, you can use @code{add-hook} and
 @code{remove-hook} to manipulate the list.  @xref{Hooks}.
 @end defvar
 
 @c Emacs 19 feature
-@defvar local-write-file-hooks
-This works just like @code{write-file-hooks}, but it is intended to be
-made buffer-local in particular buffers, and used for hooks that pertain
-to the file name or the way the buffer contents were obtained.
-
-The variable is marked as a permanent local, so that changing the major
-mode does not alter a buffer-local value.  This is convenient for
-packages that read ``file'' contents in special ways, and set up hooks
-to save the data in a corresponding way.
-@end defvar
-
-@c Emacs 19 feature
-@defvar write-contents-hooks
-This works just like @code{write-file-hooks}, but it is intended for
+@defvar write-contents-functions
+This works just like @code{write-file-functions}, but it is intended for
 hooks that pertain to the contents of the file, as opposed to hooks that
 pertain to where the file came from.  Such hooks are usually set up by
 major modes, as buffer-local bindings for this variable.
 
 This variable automatically becomes buffer-local whenever it is set;
-switching to a new major mode always resets this variable.  When you use
-@code{add-hooks} to add an element to this hook, you should @emph{not}
-specify a non-@code{nil} @var{local} argument, since this variable is
-used @emph{only} buffer-locally.
+switching to a new major mode always resets this variable.
 @end defvar
 
 @c Emacs 19 feature
@@ -512,7 +504,7 @@ with @code{insert-file-contents}, as long as @var{replace} and
 This function works like @code{insert-file-contents} except that it does
 not do format decoding (@pxref{Format Conversion}), does not do
 character code conversion (@pxref{Coding Systems}), does not run
-@code{find-file-hooks}, does not perform automatic uncompression, and so
+@code{find-file-hook}, does not perform automatic uncompression, and so
 on.
 @end defun
 
@@ -556,7 +548,7 @@ offset from the start of the file and writes the data from there.
 
 If @var{mustbenew} is non-@code{nil}, then @code{write-region} asks
 for confirmation if @var{filename} names an existing file.
-Starting in Emacs 21, if @var{mustbenew} is the symbol @code{excl}, 
+Starting in Emacs 21, if @var{mustbenew} is the symbol @code{excl},
 then @code{write-region} does not ask for confirmation, but instead
 it signals an error @code{file-already-exists} if the file already
 exists.
@@ -873,7 +865,7 @@ name of a text file, a directory, or even another symbolic link, or it
 may be a nonexistent file name.
 
 If the file @var{filename} is not a symbolic link (or there is no such file),
-@code{file-symlink-p} returns @code{nil}.  
+@code{file-symlink-p} returns @code{nil}.
 
 @example
 @group
@@ -1102,8 +1094,10 @@ the value has the form @code{(@var{high} . @var{low})}, where @var{low}
 holds the low 16 bits.
 
 @item
-The file system number of the file system that the file is in.  This
-element and the file's inode number together give enough information to
+The file system number of the file system that the file is in.
+Depending on the magnitude of the value, this can be either an integer
+or a cons cell, in the same manner as the inode number.  This element
+and the file's inode number together give enough information to
 distinguish any two files on the system---no two files can have the same
 values for both of these numbers.
 @end enumerate
@@ -1113,11 +1107,11 @@ For example, here are the file attributes for @file{files.texi}:
 @example
 @group
 (file-attributes "files.texi")
-     @result{}  (nil 1 2235 75 
-          (8489 20284) 
-          (8489 20284) 
+     @result{}  (nil 1 2235 75
+          (8489 20284)
+          (8489 20284)
           (8489 20285)
-          14906 "-rw-rw-rw-" 
+          14906 "-rw-rw-rw-"
           nil 129500 -32252)
 @end group
 @end example
@@ -1328,6 +1322,7 @@ be an integer).  Only the low 12 bits of @var{mode} are used.
 
 @c Emacs 19 feature
 @defun set-default-file-modes mode
+@cindex umask
 This function sets the default file protection for new files created by
 Emacs and its subprocesses.  Every file created with Emacs initially has
 this protection, or a subset of it (@code{write-region} will not give a
@@ -1390,9 +1385,9 @@ and work properly on all systems without change.
 
 @menu
 * File Name Components::  The directory part of a file name, and the rest.
+* Relative File Names::   Some file names are relative to a current directory.
 * Directory Names::       A directory's name as a directory
                             is different from its name as a file.
-* Relative File Names::   Some file names are relative to a current directory.
 * File Name Expansion::   Converting relative file names to absolute ones.
 * Unique File Names::     Generating names for temporary files.
 * File Name Completion::  Finding the completions for a given file name.
@@ -1426,10 +1421,13 @@ in Emacs omits the version number, so that version numbers in Emacs are
 found mostly in directory lists.
 
 @defun file-name-directory filename
-This function returns the directory part of @var{filename} (or
-@code{nil} if @var{filename} does not include a directory part).  On
-most systems, the function returns a string ending in a slash.  On VMS,
-it returns a string ending in one of the three characters @samp{:},
+This function returns the directory part of @var{filename}, as a
+directory name (@pxref{Directory Names}), or @code{nil} if
+@var{filename} does not include a directory part.
+
+On GNU and Unix systems, a string returned by this function always
+ends in a slash.  On MSDOS it can also end in a colon.  On VMS, it
+returns a string ending in one of the three characters @samp{:},
 @samp{]}, or @samp{>}.
 
 @example
@@ -1461,6 +1459,10 @@ This function returns the nondirectory part of @var{filename}.
      @result{} "foo"
 @end group
 @group
+(file-name-nondirectory "lewis/")
+     @result{} ""
+@end group
+@group
 ;; @r{The following example is accurate only on VMS.}
 (file-name-nondirectory "[X]FOO.TMP")
      @result{} "FOO.TMP"
@@ -1468,6 +1470,18 @@ This function returns the nondirectory part of @var{filename}.
 @end example
 @end defun
 
+@defun file-name-extension filename &optional period
+This function returns @var{filename}'s final ``extension,'' if any,
+after applying @code{file-name-sans-versions} to remove any
+version/backup part.  It returns @code{nil} for extensionless file
+names such as @file{foo}.  If @var{period} is non-nil, then the
+returned value includes the period that delimits the extension, and if
+@var{filename} has no extension, the value is @code{""}.  If the last
+component of a file name begins with a @samp{.}, that @samp{.} doesn't
+count as the beginning of an extension, so, for example,
+@file{.emacs}'s ``extension'' is @code{nil}, not @samp{.emacs}.
+@end defun
+
 @defun file-name-sans-versions filename &optional keep-backup-version
 This function returns @var{filename} with any file version numbers,
 backup version numbers, or trailing tildes discarded.
@@ -1500,18 +1514,23 @@ return value, but backup version numbers are kept.
 @defun file-name-sans-extension filename
 This function returns @var{filename} minus its ``extension,'' if any.
 The extension, in a file name, is the part that starts with the last
-@samp{.} in the last name component.  For example,
+@samp{.} in the last name component, except if that @samp{.} is the
+first character of the file name's last component.  For example,
 
 @example
 (file-name-sans-extension "foo.lose.c")
      @result{} "foo.lose"
 (file-name-sans-extension "big.hack/foo")
      @result{} "big.hack/foo"
+(file-name-sans-extension "/my/home/.emacs")
+     @result{} "/my/home/.emacs"
+(file-name-sans-extension "/my/home/.emacs.el")
+     @result{} "/my/home/.emacs"
 @end example
 @end defun
 
 @ignore
-Andrew Innes says that this 
+Andrew Innes says that this
 
 @c @defvar directory-sep-char
 @c @tindex directory-sep-char
@@ -1526,25 +1545,63 @@ value of @code{?/}.
 @end defvar
 @end ignore
 
+@node Relative File Names
+@subsection Absolute and Relative File Names
+@cindex absolute file name
+@cindex relative file name
+
+  All the directories in the file system form a tree starting at the
+root directory.  A file name can specify all the directory names
+starting from the root of the tree; then it is called an @dfn{absolute}
+file name.  Or it can specify the position of the file in the tree
+relative to a default directory; then it is called a @dfn{relative} file
+name.  On Unix and GNU/Linux, an absolute file name starts with a slash
+or a tilde (@samp{~}), and a relative one does not.  On MS-DOS and
+MS-Windows, an absolute file name starts with a slash or a backslash, or
+with a drive specification @samp{@var{x}:/}, where @var{x} is the
+@dfn{drive letter}.  The rules on VMS are complicated.
+
+@defun file-name-absolute-p filename
+This function returns @code{t} if file @var{filename} is an absolute
+file name, @code{nil} otherwise.  On VMS, this function understands both
+Unix syntax and VMS syntax.
+
+@example
+@group
+(file-name-absolute-p "~rms/foo")
+     @result{} t
+@end group
+@group
+(file-name-absolute-p "rms/foo")
+     @result{} nil
+@end group
+@group
+(file-name-absolute-p "/user/rms/foo")
+     @result{} t
+@end group
+@end example
+@end defun
+
 @node Directory Names
 @comment  node-name,  next,  previous,  up
 @subsection Directory Names
 @cindex directory name
 @cindex file name of directory
 
-  A @dfn{directory name} is the name of a directory.  A directory is a
-kind of file, and it has a file name, which is related to the directory
-name but not identical to it.  (This is not quite the same as the usual
-Unix terminology.)  These two different names for the same entity are
-related by a syntactic transformation.  On most systems, this is simple:
-a directory name ends in a slash (or backslash), whereas the directory's
-name as a file lacks that slash.  On VMS, the relationship is more
-complicated.
+  A @dfn{directory name} is the name of a directory.  A directory is
+actually a kind of file, so it has a file name, which is related to
+the directory name but not identical to it.  (This is not quite the
+same as the usual Unix terminology.)  These two different names for
+the same entity are related by a syntactic transformation.  On GNU and
+Unix systems, this is simple: a directory name ends in a slash (or
+backslash), whereas the directory's name as a file lacks that slash.
+On MSDOS and VMS, the relationship is more complicated.
 
   The difference between a directory name and its name as a file is
 subtle but crucial.  When an Emacs variable or function argument is
 described as being a directory name, a file name of a directory is not
-acceptable.
+acceptable.  When @code{file-name-directory} returns a string, that is
+always a directory name.
 
   The following two functions convert between directory names and file
 names.  They do nothing special with environment variable substitutions
@@ -1580,6 +1637,38 @@ to @file{[X]Y.DIR.1}.
 @end example
 @end defun
 
+  Given a directory name, you can combine it with a relative file name
+using @code{concat}:
+
+@example
+(concat @var{dirname} @var{relfile})
+@end example
+
+@noindent
+Be sure to verify that the file name is relative before doing that.
+If you use an absolute file name, the results could be syntactically
+invalid or refer to the wrong file.
+
+  If you want to use a directory file name in making such a
+combination, you must first convert it to a directory name using
+@code{file-name-as-directory}:
+
+@example
+(concat (file-name-as-directory @var{dirfile}) @var{relfile})
+@end example
+
+@noindent
+Don't try concatenating a slash by hand, as in
+
+@example
+;;; @r{Wrong!}
+(concat @var{dirfile} "/" @var{relfile})
+@end example
+
+@noindent
+because this is not portable.  Always use
+@code{file-name-as-directory}.
+
 @cindex directory name abbreviation
   Directory name abbreviations are useful for directories that are
 normally accessed through symbolic links.  Sometimes the users recognize
@@ -1613,47 +1702,11 @@ and so on.
   To convert a directory name to its abbreviation, use this
 function:
 
-@defun abbreviate-file-name dirname
+@defun abbreviate-file-name filename
 This function applies abbreviations from @code{directory-abbrev-alist}
 to its argument, and substitutes @samp{~} for the user's home
-directory.
-@end defun
-
-@node Relative File Names
-@subsection Absolute and Relative File Names
-@cindex absolute file name
-@cindex relative file name
-
-  All the directories in the file system form a tree starting at the
-root directory.  A file name can specify all the directory names
-starting from the root of the tree; then it is called an @dfn{absolute}
-file name.  Or it can specify the position of the file in the tree
-relative to a default directory; then it is called a @dfn{relative} file
-name.  On Unix and GNU/Linux, an absolute file name starts with a slash
-or a tilde (@samp{~}), and a relative one does not.  On MS-DOS and
-MS-Windows, an absolute file name starts with a slash or a backslash, or
-with a drive specification @samp{@var{x}:/}, where @var{x} is the
-@dfn{drive letter}.  The rules on VMS are complicated.
-
-@defun file-name-absolute-p filename
-This function returns @code{t} if file @var{filename} is an absolute
-file name, @code{nil} otherwise.  On VMS, this function understands both
-Unix syntax and VMS syntax.
-
-@example
-@group
-(file-name-absolute-p "~rms/foo")
-     @result{} t
-@end group
-@group
-(file-name-absolute-p "rms/foo")
-     @result{} nil
-@end group
-@group
-(file-name-absolute-p "/user/rms/foo")
-     @result{} t
-@end group
-@end example
+directory.  You can use it for directory names and for file names,
+because it recognizes abbreviations even as part of the name.
 @end defun
 
 @node File Name Expansion
@@ -1921,12 +1974,12 @@ default directory, and has five files whose names begin with @samp{f}:
 @example
 @group
 (file-name-all-completions "f" "")
-     @result{} ("foo" "file~" "file.c.~2~" 
+     @result{} ("foo" "file~" "file.c.~2~"
                 "file.c.~1~" "file.c")
 @end group
 
 @group
-(file-name-all-completions "fo" "")  
+(file-name-all-completions "fo" "")
      @result{} ("foo")
 @end group
 @end example
@@ -1983,6 +2036,11 @@ completion-ignored-extensions
      @result{} (".o" ".elc" "~" ".dvi")
 @end group
 @end example
+
+If an element of @code{completion-ignored-extensions} ends in a slash
+@samp{/}, it signals a directory.  The elements which do @emph{not} end
+in a slash will never match a directory; thus, the above value will not
+filter out a directory named @file{foo.elc}.
 @end defopt
 
 @node Standard File Names
@@ -2068,7 +2126,7 @@ then the user will probably be happier if you do sort the names.
 @group
 (directory-files "~lewis")
      @result{} ("#foo#" "#foo.el#" "." ".."
-         "dired-mods.el" "files.texi" 
+         "dired-mods.el" "files.texi"
          "files.texi.~1~")
 @end group
 @end example
@@ -2182,7 +2240,7 @@ calling @var{handler}.
 
 The first argument given to @var{handler} is the name of the primitive;
 the remaining arguments are the arguments that were passed to that
-operation.  (The first of these arguments is typically the file name
+primitive.  (The first of these arguments is most often the file name
 itself.)  For example, if you do this:
 
 @example
@@ -2197,24 +2255,48 @@ called like this:
 (funcall @var{handler} 'file-exists-p @var{filename})
 @end example
 
+When a function takes two or more arguments that must be file names,
+it checks each of those names for a handler.  For example, if you do
+this:
+
+@example
+(expand-file-name @var{filename} @var{dirname})
+@end example
+
+@noindent
+then it checks for a handler for @var{filename} and then for a handler
+for @var{dirname}.  In either case, the @var{handler} is called like
+this:
+
+@example
+(funcall @var{handler} 'expand-file-name @var{filename} @var{dirname})
+@end example
+
+@noindent
+The @var{handler} then needs to figure out whether to handle
+@var{filename} or @var{dirname}.
+
 Here are the operations that a magic file name handler gets to handle:
 
 @ifnottex
 @noindent
-@code{add-name-to-file}, @code{copy-file}, @code{delete-directory},
+@code{access-file}, @code{add-name-to-file},
+@code{byte-compiler-base-file-name},@*
+@code{copy-file}, @code{delete-directory},
 @code{delete-file},
 @code{diff-latest-backup-file},
 @code{directory-file-name},
 @code{directory-files},
+@code{directory-files-and-attributes},
 @code{dired-call-process},
-@code{dired-compress-file}, @code{dired-uncache},
+@code{dired-compress-file}, @code{dired-uncache},@*
 @code{expand-file-name},
-@code{file-accessible-directory-p},@*
+@code{file-accessible-directory-p},
 @code{file-attributes},
 @code{file-directory-p},
-@code{file-executable-p}, @code{file-exists-p},@*
+@code{file-executable-p}, @code{file-exists-p},
 @code{file-local-copy},
-@code{file-modes}, @code{file-name-all-completions},@*
+@code{file-modes}, @code{file-name-all-completions},
 @code{file-name-as-directory},
 @code{file-name-completion},
 @code{file-name-directory},
@@ -2224,12 +2306,16 @@ Here are the operations that a magic file name handler gets to handle:
 @code{file-readable-p}, @code{file-regular-p}, @code{file-symlink-p},
 @code{file-truename}, @code{file-writable-p},
 @code{find-backup-file-name},
-@code{get-file-buffer},@*
+@code{find-file-noselect},@*
+@code{get-file-buffer},
 @code{insert-directory},
-@code{insert-file-contents},
+@code{insert-file-contents},@*
 @code{load}, @code{make-directory},
-@code{make-symbolic-link}, @code{rename-file}, @code{set-file-modes},
-@code{set-visited-file-modtime}, @code{shell-command},@*
+@code{make-directory-internal},
+@code{make-symbolic-link},@*
+@code{rename-file}, @code{set-file-modes},
+@code{set-visited-file-modtime}, @code{shell-command},
+@code{substitute-in-file-name},@*
 @code{unhandled-file-name-directory},
 @code{vc-registered},
 @code{verify-visited-file-modtime},@*
@@ -2238,11 +2324,14 @@ Here are the operations that a magic file name handler gets to handle:
 @iftex
 @noindent
 @flushleft
-@code{add-name-to-file}, @code{copy-file}, @code{delete-directory},
+@code{access-file}, @code{add-name-to-file},
+@code{byte-com@discretionary{}{}{}piler-base-file-name},
+@code{copy-file}, @code{delete-directory},
 @code{delete-file},
 @code{diff-latest-backup-file},
 @code{directory-file-name},
 @code{directory-files},
+@code{directory-files-and-at@discretionary{}{}{}tributes},
 @code{dired-call-process},
 @code{dired-compress-file}, @code{dired-uncache},
 @code{expand-file-name},
@@ -2261,12 +2350,16 @@ Here are the operations that a magic file name handler gets to handle:
 @code{file-readable-p}, @code{file-regular-p}, @code{file-symlink-p},
 @code{file-truename}, @code{file-writable-p},
 @code{find-backup-file-name},
+@code{find-file-noselect},
 @code{get-file-buffer},
 @code{insert-directory},
 @code{insert-file-contents},
 @code{load}, @code{make-direc@discretionary{}{}{}tory},
-@code{make-symbolic-link}, @code{rename-file}, @code{set-file-modes},
+@code{make-direc@discretionary{}{}{}tory-internal},
+@code{make-symbolic-link},
+@code{rename-file}, @code{set-file-modes},
 @code{set-visited-file-modtime}, @code{shell-command},
+@code{substitute-in-file-name},
 @code{unhandled-file-name-directory},
 @code{vc-regis@discretionary{}{}{}tered},
 @code{verify-visited-file-modtime},
@@ -2295,7 +2388,7 @@ for an operation it does not recognize.  Here's one way to do this:
         @dots{}
         ;; @r{Handle any operation we don't know about.}
         (t (let ((inhibit-file-name-handlers
-                  (cons 'my-file-handler 
+                  (cons 'my-file-handler
                         (and (eq inhibit-file-name-operation operation)
                              inhibit-file-name-handlers)))
                  (inhibit-file-name-operation operation))