I use the following structure in my emacs config: For each programming mode I use, I maintain configuration in a file called programming-mode-config.el. (So python configuration will go into python-mode-config.el etc).
Earlier, I used to require each of these files in my init.el. The drawback of this approach was that my start-up time was huge. So this weekend, I sat down and converted all the requires into autoloads. Now my init file looks like this:
(autoload 'python-mode "python-mode-config" "Load python config" t)
Thus python config will not be loaded until I open a python file. This helped bring down my start-up time to about 1 second, but it doesn’t work properly in all cases. For example,
(autoload 'erc "erc-mode-config" "Load configuration for ERC" t)
does not load my erc tweaks at all. Looking at the autoload documentation, it states that:
Define FUNCTION to autoload from FILE.
...
If FUNCTION is already defined other than as an autoload,
this does nothing and returns nil.
So I’m guessing that the erc config is not loaded because ERC comes ‘in-built’ with emacs whereas python-mode is a plugin I use. Is there any way I can get my erc configuration to load only when I actually use erc? The only other alternative I see is using eval-after-load, but it would be rather painful to put every tiny bit of my customization into an eval-after-load.
I’m afraid it might also be that I haven’t grokked autoloads properly. Any help would be appreciated.
autoloadis intended to be used to load functions from a certain file, not to load additional functionality – which is what it looks like you’re trying to do.Use
eval-after-loadinstead:That tells Emacs to load the
erc-mode-configlibrary after the"erc"file has been loaded – which is what you want. You could also use'(require 'erc-mode-config)if you have aprovidestatement inside of it.The correct use of
autoloadis to load the actual file that contains the symbol. So, by havingYou were telling Emacs to find the function
ercby loading the"erc-mode-config"library, which isn’t where theercfunction is defined. Also, the docstring is for the function in question, so theautoloadstatement above makes the help string forercbe"Load configuration for ERC"– which is also incorrect.I’m guessing your first
autoloadexample works because you have a(require 'python)statement in your config file… but that’s just a guess.