summaryrefslogtreecommitdiff
path: root/devdocs/elisp/hooks-for-loading.html
blob: 34e2c379ab9a6a1a88e60ca5906a00a07bd15e0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 <h3 class="section">Hooks for Loading</h3>   <p>You can ask for code to be executed each time Emacs loads a library, by using the variable <code>after-load-functions</code>: </p> <dl> <dt id="after-load-functions">Variable: <strong>after-load-functions</strong>
</dt> <dd><p>This abnormal hook is run after loading a file. Each function in the hook is called with a single argument, the absolute filename of the file that was just loaded. </p></dd>
</dl> <p>If you want code to be executed when a <em>particular</em> library is loaded, use the macro <code>with-eval-after-load</code>: </p> <dl> <dt id="with-eval-after-load">Macro: <strong>with-eval-after-load</strong> <em>library body…</em>
</dt> <dd>
<p>This macro arranges to evaluate <var>body</var> at the end of loading the file <var>library</var>, each time <var>library</var> is loaded. If <var>library</var> is already loaded, it evaluates <var>body</var> right away. </p> <p>You don’t need to give a directory or extension in the file name <var>library</var>. Normally, you just give a bare file name, like this: </p> <div class="example"> <pre class="example">(with-eval-after-load "js" (define-key js-mode-map "\C-c\C-c" 'js-eval))
</pre>
</div> <p>To restrict which files can trigger the evaluation, include a directory or an extension or both in <var>library</var>. Only a file whose absolute true name (i.e., the name with all symbolic links chased out) matches all the given name components will match. In the following example, <samp>my_inst.elc</samp> or <samp>my_inst.elc.gz</samp> in some directory <code>..../foo/bar</code> will trigger the evaluation, but not <samp>my_inst.el</samp>: </p> <div class="example"> <pre class="example">(with-eval-after-load "foo/bar/my_inst.elc" …)
</pre>
</div> <p><var>library</var> can also be a feature (i.e., a symbol), in which case <var>body</var> is evaluated at the end of any file where <code>(provide <var>library</var>)</code> is called. </p> <p>An error in <var>body</var> does not undo the load, but does prevent execution of the rest of <var>body</var>. </p>
</dd>
</dl> <p>Normally, well-designed Lisp programs should not use <code>with-eval-after-load</code>. If you need to examine and set the variables defined in another library (those meant for outside use), you can do it immediately—there is no need to wait until the library is loaded. If you need to call functions defined by that library, you should load the library, preferably with <code>require</code> (see <a href="named-features">Named Features</a>). </p><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/Hooks-for-Loading.html" class="_attribution-link">https://www.gnu.org/software/emacs/manual/html_node/elisp/Hooks-for-Loading.html</a>
  </p>
</div>