问题描述
我该怎么弄清楚?
我将 delete-trailing-whitespace
添加到 before-save-hook
c-mode-common-hook
,但它看起来像 delete-trailing-whitespace
每个文件都被调用,而不仅仅是缓冲区使用c模式和派生词。
I added delete-trailing-whitespace
to the before-save-hook
in my c-mode-common-hook
, but it looks like delete-trailing-whitespace
is getting called for every file, not just buffers using c-mode and derivatives.
我可以在$ / code
Can I make the before-save-hook
buffer local?
推荐答案
将其添加到 write-contents-functions
而不是
(add-hook 'c-mode-common-hook
(lambda()
(add-hook 'write-contents-functions
(lambda()
(save-excursion
(delete-trailing-whitespace)))
nil t)))
正如Emacs Lisp参考手册所说明的那样:
As the Emacs Lisp Reference Manual explains:
这在Emacs 24.2.1中适用于我(即,它从C文件中删除所有尾随空格,但在所有其他文件类型中保留尾随空格)。
This works properly for me in Emacs 24.2.1 (i.e., it deletes all trailing whitespace from C files but preserves trailing whitespace in all other file types).
这篇关于emacs:是先保存一个局部变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!