Mercurial > emacs
comparison lispref/files.texi @ 46956:2c01ee3e5305
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.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Mon, 19 Aug 2002 18:43:18 +0000 |
parents | 87962bf716e3 |
children | e0d6f0b369d1 |
comparison
equal
deleted
inserted
replaced
46955:2a1651fdb0b5 | 46956:2c01ee3e5305 |
---|---|
1382 syntax. This enables Lisp programs to specify file names in Unix syntax | 1382 syntax. This enables Lisp programs to specify file names in Unix syntax |
1383 and work properly on all systems without change. | 1383 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. |
1393 * Standard File Names:: If your package uses a fixed file name, | 1393 * Standard File Names:: If your package uses a fixed file name, |
1394 how to handle various operating systems simply. | 1394 how to handle various operating systems simply. |
1418 has a version number, but most of the time the file name actually used | 1418 has a version number, but most of the time the file name actually used |
1419 in Emacs omits the version number, so that version numbers in Emacs are | 1419 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 |
1430 @group | 1433 @group |
1431 (file-name-directory "lewis/foo") ; @r{Unix example} | 1434 (file-name-directory "lewis/foo") ; @r{Unix example} |
1451 @result{} "foo" | 1454 @result{} "foo" |
1452 @end group | 1455 @end group |
1453 @group | 1456 @group |
1454 (file-name-nondirectory "foo") | 1457 (file-name-nondirectory "foo") |
1455 @result{} "foo" | 1458 @result{} "foo" |
1459 @end group | |
1460 @group | |
1461 (file-name-nondirectory "lewis/") | |
1462 @result{} "" | |
1456 @end group | 1463 @end group |
1457 @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" |
1461 @end group | 1468 @end group |
1462 @end example | 1469 @end example |
1463 @end defun | 1470 @end defun |
1464 | |
1465 @defun file-name-sans-versions filename &optional keep-backup-version | |
1466 This function returns @var{filename} with any file version numbers, | |
1467 backup version numbers, or trailing tildes discarded. | |
1468 | |
1469 If @var{keep-backup-version} is non-@code{nil}, then true file version | |
1470 numbers understood as such by the file system are discarded from the | |
1471 return value, but backup version numbers are kept. | |
1472 | |
1473 @example | |
1474 @group | |
1475 (file-name-sans-versions "~rms/foo.~1~") | |
1476 @result{} "~rms/foo" | |
1477 @end group | |
1478 @group | |
1479 (file-name-sans-versions "~rms/foo~") | |
1480 @result{} "~rms/foo" | |
1481 @end group | |
1482 @group | |
1483 (file-name-sans-versions "~rms/foo") | |
1484 @result{} "~rms/foo" | |
1485 @end group | |
1486 @group | |
1487 ;; @r{The following example applies to VMS only.} | |
1488 (file-name-sans-versions "foo;23") | |
1489 @result{} "foo" | |
1490 @end group | |
1491 @end example | |
1492 @end defun | |
1493 | |
1494 @defun file-name-sans-extension filename | |
1495 This function returns @var{filename} minus its ``extension,'' if any. | |
1496 The extension, in a file name, is the part that starts with the last | |
1497 @samp{.} in the last name component, except if that @samp{.} is the | |
1498 first character of the file name's last component. For example, | |
1499 | |
1500 @example | |
1501 (file-name-sans-extension "foo.lose.c") | |
1502 @result{} "foo.lose" | |
1503 (file-name-sans-extension "big.hack/foo") | |
1504 @result{} "big.hack/foo" | |
1505 (file-name-sans-extension "/my/home/.emacs") | |
1506 @result{} "/my/home.emacs" | |
1507 (file-name-sans-extension "/my/home/.emacs.el") | |
1508 @result{} "/my/home/.emacs" | |
1509 @end example | |
1510 @end defun | |
1511 | |
1512 @ignore | |
1513 Andrew Innes says that this | |
1514 | |
1515 @c @defvar directory-sep-char | |
1516 @c @tindex directory-sep-char | |
1517 This variable holds the character that Emacs normally uses to separate | |
1518 file name components. The default value is @code{?/}, but on MS-Windows | |
1519 you can set it to @code{?\\}; then the functions that transform file names | |
1520 use backslashes in their output. | |
1521 | |
1522 File names using backslashes work as input to Lisp primitives even on | |
1523 MS-DOS and MS-Windows, even if @code{directory-sep-char} has its default | |
1524 value of @code{?/}. | |
1525 @end defvar | |
1526 @end ignore | |
1527 | 1471 |
1528 @defun file-name-extension filename &optional period | 1472 @defun file-name-extension filename &optional period |
1529 This function returns @var{filename}'s final ``extension,'' if any, | 1473 This function returns @var{filename}'s final ``extension,'' if any, |
1530 after applying @code{file-name-sans-versions} to remove any | 1474 after applying @code{file-name-sans-versions} to remove any |
1531 version/backup part. It returns @code{nil} for extensionless file | 1475 version/backup part. It returns @code{nil} for extensionless file |
1535 component of a file name begins with a @samp{.}, that @samp{.} doesn't | 1479 component of a file name begins with a @samp{.}, that @samp{.} doesn't |
1536 count as the beginning of an extension, so, for example, | 1480 count as the beginning of an extension, so, for example, |
1537 @file{.emacs}'s ``extension'' is @code{nil}, not @samp{.emacs}. | 1481 @file{.emacs}'s ``extension'' is @code{nil}, not @samp{.emacs}. |
1538 @end defun | 1482 @end defun |
1539 | 1483 |
1540 @node Directory Names | 1484 @defun file-name-sans-versions filename &optional keep-backup-version |
1541 @comment node-name, next, previous, up | 1485 This function returns @var{filename} with any file version numbers, |
1542 @subsection Directory Names | 1486 backup version numbers, or trailing tildes discarded. |
1543 @cindex directory name | 1487 |
1544 @cindex file name of directory | 1488 If @var{keep-backup-version} is non-@code{nil}, then true file version |
1545 | 1489 numbers understood as such by the file system are discarded from the |
1546 A @dfn{directory name} is the name of a directory. A directory is a | 1490 return value, but backup version numbers are kept. |
1547 kind of file, and it has a file name, which is related to the directory | 1491 |
1548 name but not identical to it. (This is not quite the same as the usual | 1492 @example |
1549 Unix terminology.) These two different names for the same entity are | 1493 @group |
1550 related by a syntactic transformation. On most systems, this is simple: | 1494 (file-name-sans-versions "~rms/foo.~1~") |
1551 a directory name ends in a slash (or backslash), whereas the directory's | 1495 @result{} "~rms/foo" |
1552 name as a file lacks that slash. On VMS, the relationship is more | 1496 @end group |
1553 complicated. | 1497 @group |
1554 | 1498 (file-name-sans-versions "~rms/foo~") |
1555 The difference between a directory name and its name as a file is | 1499 @result{} "~rms/foo" |
1556 subtle but crucial. When an Emacs variable or function argument is | 1500 @end group |
1557 described as being a directory name, a file name of a directory is not | 1501 @group |
1558 acceptable. | 1502 (file-name-sans-versions "~rms/foo") |
1559 | 1503 @result{} "~rms/foo" |
1560 The following two functions convert between directory names and file | 1504 @end group |
1561 names. They do nothing special with environment variable substitutions | 1505 @group |
1562 such as @samp{$HOME}, and the constructs @samp{~}, and @samp{..}. | 1506 ;; @r{The following example applies to VMS only.} |
1563 | 1507 (file-name-sans-versions "foo;23") |
1564 @defun file-name-as-directory filename | 1508 @result{} "foo" |
1565 This function returns a string representing @var{filename} in a form | 1509 @end group |
1566 that the operating system will interpret as the name of a directory. On | 1510 @end example |
1567 most systems, this means appending a slash to the string (if it does not | 1511 @end defun |
1568 already end in one). On VMS, the function converts a string of the form | 1512 |
1569 @file{[X]Y.DIR.1} to the form @file{[X.Y]}. | 1513 @defun file-name-sans-extension filename |
1570 | 1514 This function returns @var{filename} minus its ``extension,'' if any. |
1571 @example | 1515 The extension, in a file name, is the part that starts with the last |
1572 @group | 1516 @samp{.} in the last name component, except if that @samp{.} is the |
1573 (file-name-as-directory "~rms/lewis") | 1517 first character of the file name's last component. For example, |
1574 @result{} "~rms/lewis/" | 1518 |
1575 @end group | 1519 @example |
1576 @end example | 1520 (file-name-sans-extension "foo.lose.c") |
1577 @end defun | 1521 @result{} "foo.lose" |
1578 | 1522 (file-name-sans-extension "big.hack/foo") |
1579 @defun directory-file-name dirname | 1523 @result{} "big.hack/foo" |
1580 This function returns a string representing @var{dirname} in a form that | 1524 (file-name-sans-extension "/my/home/.emacs") |
1581 the operating system will interpret as the name of a file. On most | 1525 @result{} "/my/home.emacs" |
1582 systems, this means removing the final slash (or backslash) from the | 1526 (file-name-sans-extension "/my/home/.emacs.el") |
1583 string. On VMS, the function converts a string of the form @file{[X.Y]} | 1527 @result{} "/my/home/.emacs" |
1584 to @file{[X]Y.DIR.1}. | 1528 @end example |
1585 | 1529 @end defun |
1586 @example | 1530 |
1587 @group | 1531 @ignore |
1588 (directory-file-name "~lewis/") | 1532 Andrew Innes says that this |
1589 @result{} "~lewis" | 1533 |
1590 @end group | 1534 @c @defvar directory-sep-char |
1591 @end example | 1535 @c @tindex directory-sep-char |
1592 @end defun | 1536 This variable holds the character that Emacs normally uses to separate |
1593 | 1537 file name components. The default value is @code{?/}, but on MS-Windows |
1594 @cindex directory name abbreviation | 1538 you can set it to @code{?\\}; then the functions that transform file names |
1595 Directory name abbreviations are useful for directories that are | 1539 use backslashes in their output. |
1596 normally accessed through symbolic links. Sometimes the users recognize | 1540 |
1597 primarily the link's name as ``the name'' of the directory, and find it | 1541 File names using backslashes work as input to Lisp primitives even on |
1598 annoying to see the directory's ``real'' name. If you define the link | 1542 MS-DOS and MS-Windows, even if @code{directory-sep-char} has its default |
1599 name as an abbreviation for the ``real'' name, Emacs shows users the | 1543 value of @code{?/}. |
1600 abbreviation instead. | |
1601 | |
1602 @defvar directory-abbrev-alist | |
1603 The variable @code{directory-abbrev-alist} contains an alist of | |
1604 abbreviations to use for file directories. Each element has the form | |
1605 @code{(@var{from} . @var{to})}, and says to replace @var{from} with | |
1606 @var{to} when it appears in a directory name. The @var{from} string is | |
1607 actually a regular expression; it should always start with @samp{^}. | |
1608 The function @code{abbreviate-file-name} performs these substitutions. | |
1609 | |
1610 You can set this variable in @file{site-init.el} to describe the | |
1611 abbreviations appropriate for your site. | |
1612 | |
1613 Here's an example, from a system on which file system @file{/home/fsf} | |
1614 and so on are normally accessed through symbolic links named @file{/fsf} | |
1615 and so on. | |
1616 | |
1617 @example | |
1618 (("^/home/fsf" . "/fsf") | |
1619 ("^/home/gp" . "/gp") | |
1620 ("^/home/gd" . "/gd")) | |
1621 @end example | |
1622 @end defvar | 1544 @end defvar |
1623 | 1545 @end ignore |
1624 To convert a directory name to its abbreviation, use this | |
1625 function: | |
1626 | |
1627 @defun abbreviate-file-name dirname | |
1628 This function applies abbreviations from @code{directory-abbrev-alist} | |
1629 to its argument, and substitutes @samp{~} for the user's home | |
1630 directory. | |
1631 @end defun | |
1632 | 1546 |
1633 @node Relative File Names | 1547 @node Relative File Names |
1634 @subsection Absolute and Relative File Names | 1548 @subsection Absolute and Relative File Names |
1635 @cindex absolute file name | 1549 @cindex absolute file name |
1636 @cindex relative file name | 1550 @cindex relative file name |
1663 @group | 1577 @group |
1664 (file-name-absolute-p "/user/rms/foo") | 1578 (file-name-absolute-p "/user/rms/foo") |
1665 @result{} t | 1579 @result{} t |
1666 @end group | 1580 @end group |
1667 @end example | 1581 @end example |
1582 @end defun | |
1583 | |
1584 @node Directory Names | |
1585 @comment node-name, next, previous, up | |
1586 @subsection Directory Names | |
1587 @cindex directory name | |
1588 @cindex file name of directory | |
1589 | |
1590 A @dfn{directory name} is the name of a directory. A directory is | |
1591 actually a kind of file, so it has a file name, which is related to | |
1592 the directory name but not identical to it. (This is not quite the | |
1593 same as the usual Unix terminology.) These two different names for | |
1594 the same entity are related by a syntactic transformation. On GNU and | |
1595 Unix systems, this is simple: a directory name ends in a slash (or | |
1596 backslash), whereas the directory's name as a file lacks that slash. | |
1597 On MSDOS and VMS, the relationship is more complicated. | |
1598 | |
1599 The difference between a directory name and its name as a file is | |
1600 subtle but crucial. When an Emacs variable or function argument is | |
1601 described as being a directory name, a file name of a directory is not | |
1602 acceptable. When @code{file-name-directory} returns a string, that is | |
1603 always a directory name. | |
1604 | |
1605 The following two functions convert between directory names and file | |
1606 names. They do nothing special with environment variable substitutions | |
1607 such as @samp{$HOME}, and the constructs @samp{~}, and @samp{..}. | |
1608 | |
1609 @defun file-name-as-directory filename | |
1610 This function returns a string representing @var{filename} in a form | |
1611 that the operating system will interpret as the name of a directory. On | |
1612 most systems, this means appending a slash to the string (if it does not | |
1613 already end in one). On VMS, the function converts a string of the form | |
1614 @file{[X]Y.DIR.1} to the form @file{[X.Y]}. | |
1615 | |
1616 @example | |
1617 @group | |
1618 (file-name-as-directory "~rms/lewis") | |
1619 @result{} "~rms/lewis/" | |
1620 @end group | |
1621 @end example | |
1622 @end defun | |
1623 | |
1624 @defun directory-file-name dirname | |
1625 This function returns a string representing @var{dirname} in a form that | |
1626 the operating system will interpret as the name of a file. On most | |
1627 systems, this means removing the final slash (or backslash) from the | |
1628 string. On VMS, the function converts a string of the form @file{[X.Y]} | |
1629 to @file{[X]Y.DIR.1}. | |
1630 | |
1631 @example | |
1632 @group | |
1633 (directory-file-name "~lewis/") | |
1634 @result{} "~lewis" | |
1635 @end group | |
1636 @end example | |
1637 @end defun | |
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 | |
1671 @cindex directory name abbreviation | |
1672 Directory name abbreviations are useful for directories that are | |
1673 normally accessed through symbolic links. Sometimes the users recognize | |
1674 primarily the link's name as ``the name'' of the directory, and find it | |
1675 annoying to see the directory's ``real'' name. If you define the link | |
1676 name as an abbreviation for the ``real'' name, Emacs shows users the | |
1677 abbreviation instead. | |
1678 | |
1679 @defvar directory-abbrev-alist | |
1680 The variable @code{directory-abbrev-alist} contains an alist of | |
1681 abbreviations to use for file directories. Each element has the form | |
1682 @code{(@var{from} . @var{to})}, and says to replace @var{from} with | |
1683 @var{to} when it appears in a directory name. The @var{from} string is | |
1684 actually a regular expression; it should always start with @samp{^}. | |
1685 The function @code{abbreviate-file-name} performs these substitutions. | |
1686 | |
1687 You can set this variable in @file{site-init.el} to describe the | |
1688 abbreviations appropriate for your site. | |
1689 | |
1690 Here's an example, from a system on which file system @file{/home/fsf} | |
1691 and so on are normally accessed through symbolic links named @file{/fsf} | |
1692 and so on. | |
1693 | |
1694 @example | |
1695 (("^/home/fsf" . "/fsf") | |
1696 ("^/home/gp" . "/gp") | |
1697 ("^/home/gd" . "/gd")) | |
1698 @end example | |
1699 @end defvar | |
1700 | |
1701 To convert a directory name to its abbreviation, use this | |
1702 function: | |
1703 | |
1704 @defun abbreviate-file-name filename | |
1705 This function applies abbreviations from @code{directory-abbrev-alist} | |
1706 to its argument, and substitutes @samp{~} for the user's home | |
1707 directory. You can use it for directory names and for file names, | |
1708 because it recognizes abbreviations even as part of the name. | |
1668 @end defun | 1709 @end defun |
1669 | 1710 |
1670 @node File Name Expansion | 1711 @node File Name Expansion |
1671 @subsection Functions that Expand Filenames | 1712 @subsection Functions that Expand Filenames |
1672 @cindex expansion of file names | 1713 @cindex expansion of file names |