Emacs c-mode 自动加载失败
我想在加载 c 模式时加载名为“my-c-setup.el”的文件。所以,我使用“自动加载”功能。
使用我的 python 设置,它运行良好:
lang.el
(autoload 'python-mode "my-python-setup" "" t)
my-python-setup.el
(require 'python)
; ...
我正在尝试对 c 模式执行相同的操作,但我确实这样做不起作用:
lang.el
(autoload 'c-mode "my-c-setup" "" t)
my-c-setup.el
(setq c-basic-offset 4)
; ...
当我尝试以 c 模式打开文件(例如 test.c)时,我有以下内容错误 :
File mode specification error: (error "Autoloading failed to define function c-mode")
I want to load my file named "my-c-setup.el" when the c-mode is loading. So, I'm using the function "autoload".
With my python setup, it works well :
lang.el
(autoload 'python-mode "my-python-setup" "" t)
my-python-setup.el
(require 'python)
; ...
I'm trying to do the same with the c-mode, but i does not work :
lang.el
(autoload 'c-mode "my-c-setup" "" t)
my-c-setup.el
(setq c-basic-offset 4)
; ...
When I try to open a file in c-mode (test.c for example), I have the following error :
File mode specification error: (error "Autoloading failed to define function c-mode")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
自动加载不是您要寻找的。它所做的只是在第一次需要时加载一些代码,这是扩展 Emacs 功能同时保持较短启动时间的便捷方法。
为了解决您的问题,我们必须考虑您真正想要做什么:您是否只是希望在某个时刻加载一些代码,或者您是否想要为
c- 中的所有缓冲区进行缓冲区本地自定义模式?
如果您只想让 Emacs 在启动时加载代码,请将代码直接放入
.emacs
文件中,或者使用load-file
或require 而不是
autoload
:load-file
只需获取一个文件名,加载该文件中的 lisp 代码并对其进行计算。因此,如果您的代码位于名为“/path/to/my-c-setup.el”的文件中,您可以将以下行放入 .emacs 中,代码将在每次启动时加载:也许您不这样做不想为您加载的每个文件提供绝对路径名。在这种情况下,您可以使用函数
load-library
,它类似于load-file
但尝试在存储在变量中的任何目录中查找给定的文件名load-path
:优点是您只需执行一次
add-to-list
部分,并且所有后续调用load-library
> 将能够在该目录中找到代码。另一种方法是提供/要求机制:您可以通过在其中放置
(provide 'feature)
调用来使 .el 文件“提供”某些功能,例如,然后放置相应的
(require 'feature)
在您的 .emacs 文件中,您的代码也会被加载:但是,如果您希望仅在缓冲区上激活
c-mode
时加载您的代码,实现的方法是通过 Emacs 的 Hook机制:大多数主要模式都提供可自定义的挂钩变量,您可以向其中添加在调用主要模式时将调用的函数。例如,c-mode 提供了
c-mode-hook
。为了在缓冲区打开 c-mode 时调用您自己的自定义,请将它们放入一个函数中,例如my-c-mode-customizations
并将以下行添加到您的 . emacs 文件:当然,您仍然需要
autoload
让 Emacs 真正找到该函数的定义。Autoload is not what you're looking for. What it does is simply load some code the first time it is needed, which is a handy way to extend Emacs' functionality while still keeping the start-up time low.
To solve your problem, we gotta think about what you really want to do: do you simply want some of your code to be loaded at some point, or do you want buffer-local customizations for ever buffer that is in
c-mode
?If you simply want Emacs to load your code at start-up, either put your code directly into your
.emacs
file or useload-file
orrequire
instead ofautoload
:load-file
simply takes a file name, loads the lisp code in that file and evaluates it. So if your code is in a file named "/path/to/my-c-setup.el", you could put the following line in your .emacs, and the code will be loaded on every start-up:Perhaps you don't want to give the absolute path name for every file you load. In that case, you could use the function
load-library
instead which is similar toload-file
but tries to find the given filename in any of the directories stored in the variableload-path
:The advantage is that you have to do the
add-to-list
part only once, and all subsequent calls toload-library
will be able to find code in that directory.An alternative way is the provide/require mechanism: you can make your .el-file "provide" some feature by putting a
(provide 'feature)
call in it, e.g.Then put an according
(require 'feature)
in your .emacs file, and your code will be loaded as well:However, if you want your code only be loaded when
c-mode
is activated on a buffer, the way to achieve that is through Emacs' Hook mechanism:Most major modes provide a customizable hook variable to which you can add functions that will be called whenever the major mode is invoked. For instance, c-mode provides
c-mode-hook
. In order for your own customizations to be called whenever c-mode is turned on for a buffer, put them in a function, say,my-c-mode-customizations
and add the following line to your .emacs file:Of course, you still need
autoload
for Emacs to actually find the definition of that function.Lisp 的 autoload 在文件加载时不会调用函数,而是告诉 lisp 该函数可用并且给定的文件提供了该函数。每当有人调用(尚未定义)函数时,就会加载该文件。
我认为
c-mode
已经定义,因此无法重新注册。Lisp's autoload does not call a function when a file is loaded but tells lisp that the function is available and that the given file provides it. Whenever someone calls the (not yet defined) function, the file is loaded.
I think that
c-mode
is already defined and thus fails to re-register.自动加载并不像您想象的那样。
http://www.gnu.org/software/emacs/elisp/ html_node/Autoload.html
您可能想要的是模式挂钩或加载后评估。
请参阅 eval-after-load 与模式挂钩了解两者之间的差异。
Autoload doesn't do what you think it does.
http://www.gnu.org/software/emacs/elisp/html_node/Autoload.html
What you probably want are mode-hooks or eval-after-load.
See eval-after-load vs. mode hook for the difference between the two.