summaryrefslogtreecommitdiff
path: root/devdocs/elisp/standard-file-names.html
blob: 68e23976c262c15dbd530b29659cff09b806a087 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 <h4 class="subsection">Standard File Names</h4> <p>Sometimes, an Emacs Lisp program needs to specify a standard file name for a particular use—typically, to hold configuration data specified by the current user. Usually, such files should be located in the directory specified by <code>user-emacs-directory</code>, which is typically <samp>~/.config/emacs/</samp> or <samp>~/.emacs.d/</samp> by default (see <a href="https://www.gnu.org/software/emacs/manual/html_node/emacs/Find-Init.html#Find-Init">How Emacs Finds Your Init File</a> in <cite>The GNU Emacs Manual</cite>). For example, abbrev definitions are stored by default in <samp>~/.config/emacs/abbrev_defs</samp> or <samp>~/.emacs.d/abbrev_defs</samp>. The easiest way to specify such a file name is to use the function <code>locate-user-emacs-file</code>. </p> <dl> <dt id="locate-user-emacs-file">Function: <strong>locate-user-emacs-file</strong> <em>base-name &amp;optional old-name</em>
</dt> <dd>
<p>This function returns an absolute file name for an Emacs-specific configuration or data file. The argument <samp>base-name</samp> should be a relative file name. The return value is the absolute name of a file in the directory specified by <code>user-emacs-directory</code>; if that directory does not exist, this function creates it. </p> <p>If the optional argument <var>old-name</var> is non-<code>nil</code>, it specifies a file in the user’s home directory, <samp>~/<var>old-name</var></samp>. If such a file exists, the return value is the absolute name of that file, instead of the file specified by <var>base-name</var>. This argument is intended to be used by Emacs packages to provide backward compatibility. For instance, prior to the introduction of <code>user-emacs-directory</code>, the abbrev file was located in <samp>~/.abbrev_defs</samp>. Here is the definition of <code>abbrev-file-name</code>: </p> <div class="example"> <pre class="example">(defcustom abbrev-file-name
  (locate-user-emacs-file "abbrev_defs" ".abbrev_defs")
  "Default name of file from which to read abbrevs."
  …
  :type 'file)
</pre>
</div> </dd>
</dl> <p>A lower-level function for standardizing file names, which <code>locate-user-emacs-file</code> uses as a subroutine, is <code>convert-standard-filename</code>. </p> <dl> <dt id="convert-standard-filename">Function: <strong>convert-standard-filename</strong> <em>filename</em>
</dt> <dd>
<p>This function returns a file name based on <var>filename</var>, which fits the conventions of the current operating system. </p> <p>On GNU and other POSIX-like systems, this simply returns <var>filename</var>. On other operating systems, it may enforce system-specific file name conventions; for example, on MS-DOS this function performs a variety of changes to enforce MS-DOS file name limitations, including converting any leading ‘<samp>.</samp>’ to ‘<samp>_</samp>’ and truncating to three characters after the ‘<samp>.</samp>’. </p> <p>The recommended way to use this function is to specify a name which fits the conventions of GNU and Unix systems, and pass it to <code>convert-standard-filename</code>. </p>
</dd>
</dl><div class="_attribution">
  <p class="_attribution-p">
    Copyright &copy; 1990-1996, 1998-2022 Free Software Foundation, Inc. <br>Licensed under the GNU GPL license.<br>
    <a href="https://www.gnu.org/software/emacs/manual/html_node/elisp/Standard-File-Names.html" class="_attribution-link">https://www.gnu.org/software/emacs/manual/html_node/elisp/Standard-File-Names.html</a>
  </p>
</div>