【问题标题】:emacs only delete-trailing-whitespace while saving in programming mode在编程模式下保存时,emacs 仅删除尾随空格
【发布时间】:2013-10-11 01:02:28
【问题描述】:

以下行在保存的同时删除所有训练空白。

(add-hook 'write-file-hooks 'delete-trailing-whitespace)

但我想只在我处于编程模式时才挂钩此功能,所以我做到了

(defun nuke_traling ()
  (add-hook 'write-file-hooks 'delete-trailing-whitespace) 
)

(add-hook 'prog-mode-hook 'nuke_traling)

不停止不处于编程模式。

【问题讨论】:

    标签: emacs whitespace mode


    【解决方案1】:

    您需要将变量缓冲区设为本地:

    (defun nuke_traling ()
        (make-variable-buffer-local 'write-file-hooks)
        (add-hook 'write-file-hooks 'delete-trailing-whitespace))
    

    但我建议改用before-save-hook

    (defun nuke_traling ()
        (add-to-list 'before-save-hook 'delete-trailing-whitespace))
    

    write-file-hooks 如果用作文件局部变量可能会有风险,文档建议使用 before-save-hook 代替您想做的事情。

    【讨论】:

    • make-variable-buffer-local 几乎不应该从函数中调用。您的意思可能是make-local-variable。但这不应该用在钩子上。相反,您想使用 add-hooklocal 参数。
    【解决方案2】:

    是的,因为一旦您进入prog-mode 模式,您就会将该函数添加到write-file-hooks,它仍然保留在那里。并且该钩子适用于写入任何文件,无论其缓冲区的模式如何。

    您可以添加一个测试模式的函数,而不是把那个简单的函数放在钩子上,并且只在您想要这样做的模式时删除空格。

    否则您需要将write-file-hooks buffer-local(我怀疑您是否想要这样做 --- 更普遍地使用挂钩)。

    【讨论】:

    • “Make a hook buffer-local”自 Emacs-21 以来是一个过时的概念。所有的钩子都有一个全局部分和一个局部缓冲区部分。你只需要告诉add-hook你要修改哪个部分。
    【解决方案3】:

    已经提到了使钩子变量缓冲区本地化。不要那样做。或者更确切地说,不要使用make-local-variable

    普通的钩子机制具有内置的缓冲区本地支持——这就是add-hookLOCAL 参数的目的。当钩子运行时,它运行 both 全局 缓冲区局部值。

    因此,以问题中的示例代码为例,您可以将其更改为使用:

    (add-hook 'write-file-hooks 'delete-trailing-whitespace nil t)
    

    然后,无论何时运行 write-file-hooks,都会调用 delete-trailing-whitespace,但仅限于运行 prog-mode-hook 的缓冲区中。

    但是有更好的方法来实现这一点。

    我同意 Drew 的观点,您最好测试一下您的模式是否源自 prog-mode,并且对于 juanleon,before-save-hook 是一个更好的使用钩子。所以你可能会这样做:

    (add-hook 'before-save-hook 'my-prog-nuke-trailing-whitespace)
    
    (defun my-prog-nuke-trailing-whitespace ()
      (when (derived-mode-p 'prog-mode)
        (delete-trailing-whitespace)))
    

    但我实际上建议使用ws-trimws-butler 以更智能的方式处理此问题。

    从文件中盲目地删除所有尾随空格是结束将大量不相关行提交到版本控制存储库的好方法。提到的两个库都将确保您自己的提交没有尾随空格,没有还会在文件的其他地方引入不需要的修改。

    【讨论】:

      【解决方案4】:

      write-file-hooks 自 Emacs-22 起已过时,由 write-file-functions 取代。但是这个钩子使用起来有点微妙(因为它也可以用来执行写操作),所以我建议你改用before-save-hook。为了使其仅适用于当前缓冲区,只需为add-hooklocal 参数传递一个非零值,如下所示:

      (defun nuke_traling ()
        (add-hook 'before-save-hook #'delete-trailing-whitespace nil t))
      (add-hook 'prog-mode-hook #'nuke_traling)
      

      【讨论】:

        【解决方案5】:

        在 emacs 21 或更高版本中,您可以将此挂钩添加到特定模式,如下所示:

        (add-hook 'prog-mode-hook
                        (lambda () (add-to-list 'write-file-functions 'delete-trailing-whitespace)))
        

        【讨论】:

          【解决方案6】:

          不好的方式:

          (add-to-list 'write-file-functions 'delete-trailing-whitespace)
          

          更好的方法是使用ws-butler:

          (straight-use-package 'ws-butler)
          (add-hook 'prog-mode-hook #'ws-butler-mode)
          

          ws-butler-mode 仅在更改的行上删除空格。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-08-04
            • 1970-01-01
            • 1970-01-01
            • 2011-11-05
            • 2014-01-07
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多