开发者

How do I use autoload to correctly load custom configuration?

开发者 https://www.devze.com 2023-03-08 19:13 出处:网络
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-mo

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.


autoload is 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-load instead:

(eval-after-load "erc" '(load "erc-mode-config"))

That tells Emacs to load the erc-mode-config library after the "erc" file has been loaded - which is what you want. You could also use '(require 'erc-mode-config) if you have a provide statement inside of it.

The correct use of autoload is to load the actual file that contains the symbol. So, by having

(autoload 'erc "erc-mode-config" "Load configuration for ERC" t)

You were telling Emacs to find the function erc by loading the "erc-mode-config" library, which isn't where the erc function is defined. Also, the docstring is for the function in question, so the autoload statement above makes the help string for erc be "Load configuration for ERC" - which is also incorrect.

I'm guessing your first autoload example works because you have a (require 'python) statement in your config file... but that's just a guess.

0

精彩评论

暂无评论...
验证码 换一张
取 消