diff options
| author | Richard M. Stallman | 2002-08-19 18:43:18 +0000 |
|---|---|---|
| committer | Richard M. Stallman | 2002-08-19 18:43:18 +0000 |
| commit | 85df4f6674ee49d51e8e6a7a2265daefcb3cb7d2 (patch) | |
| tree | 12450ce8eba7d14d5ac67a17ae4a26c2be981077 | |
| parent | aac06fd4e54620965ff4cddf0297a5c92e78eb11 (diff) | |
| download | emacs-85df4f6674ee49d51e8e6a7a2265daefcb3cb7d2.tar.gz emacs-85df4f6674ee49d51e8e6a7a2265daefcb3cb7d2.zip | |
Move the node Relative Files before Directory Names. Show what
file-name-nondirectory returns when given a directory name.
Explain that abbreviate-file-name works on file names too.
Explain how to combine a directory name with a relative file name
with concat, and the pitfalls.
Update some details.
| -rw-r--r-- | lispref/files.texi | 167 |
1 files changed, 104 insertions, 63 deletions
diff --git a/lispref/files.texi b/lispref/files.texi index eb008544f4a..35955f14b40 100644 --- a/lispref/files.texi +++ b/lispref/files.texi | |||
| @@ -1384,9 +1384,9 @@ and work properly on all systems without change. | |||
| 1384 | 1384 | ||
| 1385 | @menu | 1385 | @menu |
| 1386 | * File Name Components:: The directory part of a file name, and the rest. | 1386 | * File Name Components:: The directory part of a file name, and the rest. |
| 1387 | * Relative File Names:: Some file names are relative to a current directory. | ||
| 1387 | * Directory Names:: A directory's name as a directory | 1388 | * Directory Names:: A directory's name as a directory |
| 1388 | is different from its name as a file. | 1389 | is different from its name as a file. |
| 1389 | * Relative File Names:: Some file names are relative to a current directory. | ||
| 1390 | * File Name Expansion:: Converting relative file names to absolute ones. | 1390 | * File Name Expansion:: Converting relative file names to absolute ones. |
| 1391 | * Unique File Names:: Generating names for temporary files. | 1391 | * Unique File Names:: Generating names for temporary files. |
| 1392 | * File Name Completion:: Finding the completions for a given file name. | 1392 | * File Name Completion:: Finding the completions for a given file name. |
| @@ -1420,10 +1420,13 @@ in Emacs omits the version number, so that version numbers in Emacs are | |||
| 1420 | found mostly in directory lists. | 1420 | found mostly in directory lists. |
| 1421 | 1421 | ||
| 1422 | @defun file-name-directory filename | 1422 | @defun file-name-directory filename |
| 1423 | This function returns the directory part of @var{filename} (or | 1423 | This function returns the directory part of @var{filename}, as a |
| 1424 | @code{nil} if @var{filename} does not include a directory part). On | 1424 | directory name (@pxref{Directory Names}), or @code{nil} if |
| 1425 | most systems, the function returns a string ending in a slash. On VMS, | 1425 | @var{filename} does not include a directory part. |
| 1426 | it returns a string ending in one of the three characters @samp{:}, | 1426 | |
| 1427 | On GNU and Unix systems, a string returned by this function always | ||
| 1428 | ends in a slash. On MSDOS it can also end in a colon. On VMS, it | ||
| 1429 | returns a string ending in one of the three characters @samp{:}, | ||
| 1427 | @samp{]}, or @samp{>}. | 1430 | @samp{]}, or @samp{>}. |
| 1428 | 1431 | ||
| 1429 | @example | 1432 | @example |
| @@ -1455,6 +1458,10 @@ This function returns the nondirectory part of @var{filename}. | |||
| 1455 | @result{} "foo" | 1458 | @result{} "foo" |
| 1456 | @end group | 1459 | @end group |
| 1457 | @group | 1460 | @group |
| 1461 | (file-name-nondirectory "lewis/") | ||
| 1462 | @result{} "" | ||
| 1463 | @end group | ||
| 1464 | @group | ||
| 1458 | ;; @r{The following example is accurate only on VMS.} | 1465 | ;; @r{The following example is accurate only on VMS.} |
| 1459 | (file-name-nondirectory "[X]FOO.TMP") | 1466 | (file-name-nondirectory "[X]FOO.TMP") |
| 1460 | @result{} "FOO.TMP" | 1467 | @result{} "FOO.TMP" |
| @@ -1462,6 +1469,18 @@ This function returns the nondirectory part of @var{filename}. | |||
| 1462 | @end example | 1469 | @end example |
| 1463 | @end defun | 1470 | @end defun |
| 1464 | 1471 | ||
| 1472 | @defun file-name-extension filename &optional period | ||
| 1473 | This function returns @var{filename}'s final ``extension,'' if any, | ||
| 1474 | after applying @code{file-name-sans-versions} to remove any | ||
| 1475 | version/backup part. It returns @code{nil} for extensionless file | ||
| 1476 | names such as @file{foo}. If @var{period} is non-nil, then the | ||
| 1477 | returned value includes the period that delimits the extension, and if | ||
| 1478 | @var{filename} has no extension, the value is @code{""}. If the last | ||
| 1479 | component of a file name begins with a @samp{.}, that @samp{.} doesn't | ||
| 1480 | count as the beginning of an extension, so, for example, | ||
| 1481 | @file{.emacs}'s ``extension'' is @code{nil}, not @samp{.emacs}. | ||
| 1482 | @end defun | ||
| 1483 | |||
| 1465 | @defun file-name-sans-versions filename &optional keep-backup-version | 1484 | @defun file-name-sans-versions filename &optional keep-backup-version |
| 1466 | This function returns @var{filename} with any file version numbers, | 1485 | This function returns @var{filename} with any file version numbers, |
| 1467 | backup version numbers, or trailing tildes discarded. | 1486 | backup version numbers, or trailing tildes discarded. |
| @@ -1525,16 +1544,41 @@ value of @code{?/}. | |||
| 1525 | @end defvar | 1544 | @end defvar |
| 1526 | @end ignore | 1545 | @end ignore |
| 1527 | 1546 | ||
| 1528 | @defun file-name-extension filename &optional period | 1547 | @node Relative File Names |
| 1529 | This function returns @var{filename}'s final ``extension,'' if any, | 1548 | @subsection Absolute and Relative File Names |
| 1530 | after applying @code{file-name-sans-versions} to remove any | 1549 | @cindex absolute file name |
| 1531 | version/backup part. It returns @code{nil} for extensionless file | 1550 | @cindex relative file name |
| 1532 | names such as @file{foo}. If @var{period} is non-nil, then the | 1551 | |
| 1533 | returned value includes the period that delimits the extension, and if | 1552 | All the directories in the file system form a tree starting at the |
| 1534 | @var{filename} has no extension, the value is @code{""}. If the last | 1553 | root directory. A file name can specify all the directory names |
| 1535 | component of a file name begins with a @samp{.}, that @samp{.} doesn't | 1554 | starting from the root of the tree; then it is called an @dfn{absolute} |
| 1536 | count as the beginning of an extension, so, for example, | 1555 | file name. Or it can specify the position of the file in the tree |
| 1537 | @file{.emacs}'s ``extension'' is @code{nil}, not @samp{.emacs}. | 1556 | relative to a default directory; then it is called a @dfn{relative} file |
| 1557 | name. On Unix and GNU/Linux, an absolute file name starts with a slash | ||
| 1558 | or a tilde (@samp{~}), and a relative one does not. On MS-DOS and | ||
| 1559 | MS-Windows, an absolute file name starts with a slash or a backslash, or | ||
| 1560 | with a drive specification @samp{@var{x}:/}, where @var{x} is the | ||
| 1561 | @dfn{drive letter}. The rules on VMS are complicated. | ||
| 1562 | |||
| 1563 | @defun file-name-absolute-p filename | ||
| 1564 | This function returns @code{t} if file @var{filename} is an absolute | ||
| 1565 | file name, @code{nil} otherwise. On VMS, this function understands both | ||
| 1566 | Unix syntax and VMS syntax. | ||
| 1567 | |||
| 1568 | @example | ||
| 1569 | @group | ||
| 1570 | (file-name-absolute-p "~rms/foo") | ||
| 1571 | @result{} t | ||
| 1572 | @end group | ||
| 1573 | @group | ||
| 1574 | (file-name-absolute-p "rms/foo") | ||
| 1575 | @result{} nil | ||
| 1576 | @end group | ||
| 1577 | @group | ||
| 1578 | (file-name-absolute-p "/user/rms/foo") | ||
| 1579 | @result{} t | ||
| 1580 | @end group | ||
| 1581 | @end example | ||
| 1538 | @end defun | 1582 | @end defun |
| 1539 | 1583 | ||
| 1540 | @node Directory Names | 1584 | @node Directory Names |
| @@ -1543,19 +1587,20 @@ count as the beginning of an extension, so, for example, | |||
| 1543 | @cindex directory name | 1587 | @cindex directory name |
| 1544 | @cindex file name of directory | 1588 | @cindex file name of directory |
| 1545 | 1589 | ||
| 1546 | A @dfn{directory name} is the name of a directory. A directory is a | 1590 | A @dfn{directory name} is the name of a directory. A directory is |
| 1547 | kind of file, and it has a file name, which is related to the directory | 1591 | actually a kind of file, so it has a file name, which is related to |
| 1548 | name but not identical to it. (This is not quite the same as the usual | 1592 | the directory name but not identical to it. (This is not quite the |
| 1549 | Unix terminology.) These two different names for the same entity are | 1593 | same as the usual Unix terminology.) These two different names for |
| 1550 | related by a syntactic transformation. On most systems, this is simple: | 1594 | the same entity are related by a syntactic transformation. On GNU and |
| 1551 | a directory name ends in a slash (or backslash), whereas the directory's | 1595 | Unix systems, this is simple: a directory name ends in a slash (or |
| 1552 | name as a file lacks that slash. On VMS, the relationship is more | 1596 | backslash), whereas the directory's name as a file lacks that slash. |
| 1553 | complicated. | 1597 | On MSDOS and VMS, the relationship is more complicated. |
| 1554 | 1598 | ||
| 1555 | The difference between a directory name and its name as a file is | 1599 | The difference between a directory name and its name as a file is |
| 1556 | subtle but crucial. When an Emacs variable or function argument is | 1600 | subtle but crucial. When an Emacs variable or function argument is |
| 1557 | described as being a directory name, a file name of a directory is not | 1601 | described as being a directory name, a file name of a directory is not |
| 1558 | acceptable. | 1602 | acceptable. When @code{file-name-directory} returns a string, that is |
| 1603 | always a directory name. | ||
| 1559 | 1604 | ||
| 1560 | The following two functions convert between directory names and file | 1605 | The following two functions convert between directory names and file |
| 1561 | names. They do nothing special with environment variable substitutions | 1606 | names. They do nothing special with environment variable substitutions |
| @@ -1591,6 +1636,38 @@ to @file{[X]Y.DIR.1}. | |||
| 1591 | @end example | 1636 | @end example |
| 1592 | @end defun | 1637 | @end defun |
| 1593 | 1638 | ||
| 1639 | Given a directory name, you can combine it with a relative file name | ||
| 1640 | using @code{concat}: | ||
| 1641 | |||
| 1642 | @example | ||
| 1643 | (concat @var{dirname} @var{relfile}) | ||
| 1644 | @end example | ||
| 1645 | |||
| 1646 | @noindent | ||
| 1647 | Be sure to verify that the file name is relative before doing that. | ||
| 1648 | If you use an absolute file name, the results could be syntactically | ||
| 1649 | invalid or refer to the wrong file. | ||
| 1650 | |||
| 1651 | If you want to use a directory file name in making such a | ||
| 1652 | combination, you must first convert it to a directory name using | ||
| 1653 | @code{file-name-as-directory}: | ||
| 1654 | |||
| 1655 | @example | ||
| 1656 | (concat (file-name-as-directory @var{dirfile}) @var{relfile}) | ||
| 1657 | @end example | ||
| 1658 | |||
| 1659 | @noindent | ||
| 1660 | Don't try concatenating a slash by hand, as in | ||
| 1661 | |||
| 1662 | @example | ||
| 1663 | ;;; @r{Wrong!} | ||
| 1664 | (concat @var{dirfile} "/" @var{relfile}) | ||
| 1665 | @end example | ||
| 1666 | |||
| 1667 | @noindent | ||
| 1668 | because this is not portable. Always use | ||
| 1669 | @code{file-name-as-directory}. | ||
| 1670 | |||
| 1594 | @cindex directory name abbreviation | 1671 | @cindex directory name abbreviation |
| 1595 | Directory name abbreviations are useful for directories that are | 1672 | Directory name abbreviations are useful for directories that are |
| 1596 | normally accessed through symbolic links. Sometimes the users recognize | 1673 | normally accessed through symbolic links. Sometimes the users recognize |
| @@ -1624,47 +1701,11 @@ and so on. | |||
| 1624 | To convert a directory name to its abbreviation, use this | 1701 | To convert a directory name to its abbreviation, use this |
| 1625 | function: | 1702 | function: |
| 1626 | 1703 | ||
| 1627 | @defun abbreviate-file-name dirname | 1704 | @defun abbreviate-file-name filename |
| 1628 | This function applies abbreviations from @code{directory-abbrev-alist} | 1705 | This function applies abbreviations from @code{directory-abbrev-alist} |
| 1629 | to its argument, and substitutes @samp{~} for the user's home | 1706 | to its argument, and substitutes @samp{~} for the user's home |
| 1630 | directory. | 1707 | directory. You can use it for directory names and for file names, |
| 1631 | @end defun | 1708 | because it recognizes abbreviations even as part of the name. |
| 1632 | |||
| 1633 | @node Relative File Names | ||
| 1634 | @subsection Absolute and Relative File Names | ||
| 1635 | @cindex absolute file name | ||
| 1636 | @cindex relative file name | ||
| 1637 | |||
| 1638 | All the directories in the file system form a tree starting at the | ||
| 1639 | root directory. A file name can specify all the directory names | ||
| 1640 | starting from the root of the tree; then it is called an @dfn{absolute} | ||
| 1641 | file name. Or it can specify the position of the file in the tree | ||
| 1642 | relative to a default directory; then it is called a @dfn{relative} file | ||
| 1643 | name. On Unix and GNU/Linux, an absolute file name starts with a slash | ||
| 1644 | or a tilde (@samp{~}), and a relative one does not. On MS-DOS and | ||
| 1645 | MS-Windows, an absolute file name starts with a slash or a backslash, or | ||
| 1646 | with a drive specification @samp{@var{x}:/}, where @var{x} is the | ||
| 1647 | @dfn{drive letter}. The rules on VMS are complicated. | ||
| 1648 | |||
| 1649 | @defun file-name-absolute-p filename | ||
| 1650 | This function returns @code{t} if file @var{filename} is an absolute | ||
| 1651 | file name, @code{nil} otherwise. On VMS, this function understands both | ||
| 1652 | Unix syntax and VMS syntax. | ||
| 1653 | |||
| 1654 | @example | ||
| 1655 | @group | ||
| 1656 | (file-name-absolute-p "~rms/foo") | ||
| 1657 | @result{} t | ||
| 1658 | @end group | ||
| 1659 | @group | ||
| 1660 | (file-name-absolute-p "rms/foo") | ||
| 1661 | @result{} nil | ||
| 1662 | @end group | ||
| 1663 | @group | ||
| 1664 | (file-name-absolute-p "/user/rms/foo") | ||
| 1665 | @result{} t | ||
| 1666 | @end group | ||
| 1667 | @end example | ||
| 1668 | @end defun | 1709 | @end defun |
| 1669 | 1710 | ||
| 1670 | @node File Name Expansion | 1711 | @node File Name Expansion |