Loading a full dependency can take a few milliseconds. When you start to have many dependencies, the milliseconds stack up and your Emacs will boot up slower. On the other hand, there is no reason to load all dependencies when Emacs boots. Many can be deferred and loaded only once they are actually needed.

I should say that all well-designed dependencies do this automatically. When you “load” these dependencies (using the technique in the section above), you don’t actually load their full implementation, you just evaluate a few autoload declarations.

That said, now and then you come across a package that is not as well designed, so you’ll need to set up the proper autoloads manually. You do this through the somewhat weird pattern of,

Define autoloads for the dependency Define triggers for the autoloads (maybe auto-mode-alist or keybinds) Tell Emacs what configuration you want to run after the dependency is loaded

In code, this may look like

In[4]:

(autoload 'command-from-dependency "dependency" ) (define-key global-map key #'command-from-dependency) (eval-after-load "dependency" list-of-configuration-statements)

Here, we say that if command-from-dependency is executed and dependency is not in the features list, Emacs should require dependency . We also say we want key to execute command-from-dependency , so in effect pressing key will cause dependency to be loaded.

Then we have some list-of-configuration-statements we cannot run until after dependency has been loaded: in this case, we get to use eval-after-load which does what it says on the tin: it evaluates the second argument whenever the first argument is loaded. Note that list-of-configuration-statements has to be quoted! Since it’s a regular function argument, it will get evaluated at call time, and then the result will be evaluated once the dependency is loaded.

Of course, the trigger for the autoloaded command does not need to be a key – it could be anything, including automatically calling a command whenever a file with a particular extension is opened.

In[5]:

( push '( "\\.extension\\'" . dependency-command) auto-mode-alist)

Again, keep in mind that it may not be necessary for you to lazy-load well-written modes manually – they should lazy-load themselves at the correct points in time.