Emacs c-mode 自动加载失败

发布于 2024-11-03 00:25:06 字数 685 浏览 4 评论 0原文

我想在加载 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦初启 2024-11-10 00:25:06

自动加载不是您要寻找的。它所做的只是在第一次需要时加载一些代码,这是扩展 Emacs 功能同时保持较短启动时间的便捷方法。

为了解决您的问题,我们必须考虑您真正想要做什么:您是否只是希望在某个时刻加载一些代码,或者您是否想要为 c- 中的所有缓冲区进行缓冲区本地自定义模式?

如果您只想让 Emacs 在启动时加载代码,请将代码直接放入 .emacs 文件中,或者使用 load-filerequire 而不是 autoload

load-file 只需获取一个文件名,加载该文件中的 lisp 代码并对其进行计算。因此,如果您的代码位于名为“/path/to/my-c-setup.el”的文件中,您可以将以下行放入 .emacs 中,代码将在每次启动时加载:

(load-file "/path/to/my-c-setup.el")

也许您不这样做不想为您加载的每个文件提供绝对路径名。在这种情况下,您可以使用函数 load-library ,它类似于 load-file 但尝试在存储在变量中的任何目录中查找给定的文件名load-path

(add-to-list 'load-path "/path/to")
(load-library "my-c-setup.el")

优点是您只需执行一次 add-to-list 部分,并且所有后续调用 load-library > 将能够在该目录中找到代码。

另一种方法是提供/要求机制:您可以通过在其中放置 (provide 'feature) 调用来使 .el 文件“提供”某些功能,例如,

(provide 'my-c-mode-customizations)

然后放置相应的 (require 'feature) 在您的 .emacs 文件中,您的代码也会被加载:

(require 'my-c-mode-customizations)

但是,如果您希望仅在缓冲区上激活 c-mode 时加载您的代码,实现的方法是通过 Emacs 的 Hook机制:

钩子是一个变量,您可以在其中
存储一个或多个函数
在特定场合被调用
现有程序。

大多数主要模式都提供可自定义的挂钩变量,您可以向其中添加在调用主要模式时将调用的函数。例如,c-mode 提供了c-mode-hook。为了在缓冲区打开 c-mode 时调用您自己的自定义,请将它们放入一个函数中,例如 my-c-mode-customizations 并将以下行添加到您的 . emacs 文件:

(add-hook 'c-mode-hook 'my-c-mode-customizations)

当然,您仍然需要 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 use load-file or require instead of autoload:

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:

(load-file "/path/to/my-c-setup.el")

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 to load-file but tries to find the given filename in any of the directories stored in the variable load-path:

(add-to-list 'load-path "/path/to")
(load-library "my-c-setup.el")

The advantage is that you have to do the add-to-list part only once, and all subsequent calls to load-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.

(provide 'my-c-mode-customizations)

Then put an according (require 'feature) in your .emacs file, and your code will be loaded as well:

(require 'my-c-mode-customizations)

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:

A hook is a variable where you can
store a function or functions to be
called on a particular occasion by an
existing program.

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:

(add-hook 'c-mode-hook 'my-c-mode-customizations)

Of course, you still need autoload for Emacs to actually find the definition of that function.

清醇 2024-11-10 00:25:06

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.

表情可笑 2024-11-10 00:25:06

自动加载并不像您想象的那样。
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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文