如何使用自动加载来正确加载自定义配置?
我在 emacs 配置中使用以下结构: 对于我使用的每种编程模式,我都在名为programming-mode-config.el 的文件中维护配置。 (所以 python 配置将进入 python-mode-config.el 等)。
早些时候,我曾经在 init.el 中需要这些文件。这种方法的缺点是我的启动时间很长。所以这个周末,我坐下来将所有需求转换为自动加载。现在我的初始化文件如下所示:
(autoload 'python-mode "python-mode-config" "Load python config" t)
因此,在我打开 python 文件之前,不会加载 python 配置。这有助于将我的启动时间缩短到大约 1 秒,但并非在所有情况下都能正常工作。例如,
(autoload 'erc "erc-mode-config" "Load configuration for ERC" t)
根本不加载我的 erc 调整。查看自动加载文档,它指出:
Define FUNCTION to autoload from FILE.
...
If FUNCTION is already defined other than as an autoload,
this does nothing and returns nil.
所以我猜测 erc 配置未加载,因为 ERC 是 emacs 的“内置”,而 python-mode 是我使用的插件。有什么方法可以让我的 erc 配置仅在我实际使用 erc 时加载?我看到的唯一的其他选择是使用 eval-after-load,但是将我的自定义的每一点都放入 eval-after-load 中会相当痛苦。
恐怕也可能是我没有正确地理解自动加载。任何帮助将不胜感激。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
autoload
旨在用于从某个文件加载函数,而不是加载附加功能 - 这就是您想要做的事情。使用
eval-after-load
相反:这会告诉 Emacs 在加载
"erc"
文件后加载erc-mode-config
库 - 这就是您想要的。如果其中有provide
语句,您也可以使用'(require 'erc-mode-config)
。autoload
的正确用法是加载包含该符号的实际文件。因此,通过让您告诉 Emacs 通过加载
"erc-mode-config"
库来查找函数erc
,而该库不是erc< /code> 函数已定义。此外,文档字符串适用于相关函数,因此上面的
autoload
语句使erc
的帮助字符串为“Load configuration for ERC”
- 这也是不正确的。我猜你的第一个
autoload
示例可以工作,因为你的配置文件中有一个(require 'python)
语句...但这只是一个猜测。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: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 aprovide
statement inside of it.The correct use of
autoload
is to load the actual file that contains the symbol. So, by havingYou were telling Emacs to find the function
erc
by loading the"erc-mode-config"
library, which isn't where theerc
function is defined. Also, the docstring is for the function in question, so theautoload
statement above makes the help string forerc
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.