emacs:是先保存一个局部变量? [英] emacs: is before-save-hook a local variable?

查看:81
本文介绍了emacs:是先保存一个局部变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该怎么弄清楚?

我将 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:


这个工作就像写文件 - 功能,但它适用于涉及
缓冲区内容的钩子,而不是特定的访问文件或其位置。这样的钩子通常由主要模式设置为
,作为此变量的缓冲区本地绑定。这个变量
在设置时自动变为缓冲区本地;切换到新的主要模式
始终重置此变量,但调用set-visited-file-name不会。

This works just like write-file-functions, but it is intended for hooks that pertain to the buffer's contents, not to the particular visited file or its location. Such hooks are usually set up by major modes, as buffer-local bindings for this variable. This variable automatically becomes buffer-local whenever it is set; switching to a new major mode always resets this variable, but calling set-visited-file-name does not.

这在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:是先保存一个局部变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆