21

我在我的'before-save-hook. 例如,我运行'delete-trailing-whitespace. 这是我几乎在所有场合都想要的。

但有时,我正在处理与其他人共享的文件,并且该文件已经有一堆尾随空格。如果我保存文件,我会得到一个非常令人困惑的大差异,因为我的更改隐藏在数十或数百个无意义的更改中。是的,每个人都可以告诉他们的差异工具不要显示空白更改,但这是每个人每次查看差异时都必须做的事情。我什至不想改变空格。

我有什么办法可以在不更改空格的情况下保存文件,除了启动一个没有init.el文件的新 Emacs 实例,或者使用init.el没有钩子的修改?

4

6 回答 6

14

这是我在不触发的情况下保存的方法delete-trailing-whitespace:: C-x C-q C-x C-s C-x C-q只读,保存,恢复只读

于 2019-11-25T12:43:01.310 回答
13

我想出的一个更简单的解决方案是我fundamental-mode没有安装任何挂钩,因为我希望它尽可能简单。因此,如果我想在不运行钩子的情况下保存文件,我会暂时切换到fundamental-mode.

于 2017-01-20T11:44:31.070 回答
5

根据与@Stefan 的评论讨论,这里有两种可能的(未经测试的)解决方案:

使用let

(defun save-buffer-without-dtw ()
  (interactive)
  (let ((b (current-buffer)))   ; memorize the buffer
    (with-temp-buffer ; new temp buffer to bind the global value of before-save-hook
      (let ((before-save-hook (remove 'delete-trailing-whitespace before-save-hook))) 
        (with-current-buffer b  ; go back to the current buffer, before-save-hook is now buffer-local
          (let ((before-save-hook (remove 'delete-trailing-whitespace before-save-hook)))
            (save-buffer)))))))

使用unwind-protect

(defun save-buffer-without-dtw ()
  (interactive)
  (let ((restore-global
         (memq 'delete-trailing-whitespace (default-value before-save-hook)))
        (restore-local
         (and (local-variable-p 'before-save-hook)
              (memq 'delete-trailing-whitespace before-save-hook))))
    (unwind-protect
         (progn
           (when restore-global
             (remove-hook 'before-save-hook 'delete-trailing-whitespace))
           (when restore-local
             (remove-hook 'before-save-hook 'delete-trailing-whitespace t))
           (save-buffer))
      (when restore-global
        (add-hook 'before-save-hook 'delete-trailing-whitespace))
      (when restore-local
        (add-hook 'before-save-hook 'delete-trailing-whitespace nil t)))))

第二种解决方案的问题是函数的顺序before-save-hook可能会改变。

于 2013-02-18T22:44:10.173 回答
4

这是另一个解决方案:

(defvar my-inhibit-dtw nil)
(defun my-delete-trailing-whitespace ()
  (unless my-inhibit-dtw (delete-trailing-whitespace)))
(add-hook 'before-save-hook 'my-delete-trailing-whitespace)

进而

(defun my-inhibit-dtw ()
  (interactive)
  (set (make-local-variable 'my-inhibit-dtw) t))

所以你可以M-x my-inhibit-dtw RET在你不想修剪空白的缓冲区中。

于 2013-02-19T14:08:57.293 回答
2

我写了一个受 Nicholas Douma 解决方案启发的命令。

(defun olav-save-buffer-as-is ()
  "Save file \"as is\", that is in read-only-mode."
  (interactive)
  (if buffer-read-only
      (save-buffer)
    (read-only-mode 1)
    (save-buffer)
    (read-only-mode 0)))
于 2021-01-12T13:34:45.190 回答
1

我们需要做的是从 中删除'delete-trailing-whitespacebefore-save-hook保存缓冲区,然后将其添加回来。

此代码将执行此操作,但只有在它开始时才删除和添加它。

;; save the buffer, removing and readding the 'delete-trailing-whitespace function
;; to 'before-save-hook if it's there
(defun save-buffer-no-delete-trailing-whitespace ()
  (interactive)
  (let ((normally-should-delete-trailing-whitespace (memq 'delete-trailing-whitespace before-save-hook)))
    (when normally-should-delete-trailing-whitespace
      (remove-hook 'before-save-hook 'delete-trailing-whitespace))
    (save-buffer)
    (when normally-should-delete-trailing-whitespace
      (add-hook 'before-save-hook 'delete-trailing-whitespace))))
(global-set-key (kbd "C-c C-s") 'save-buffer-no-delete-trailing-whitespace)

为方便起见,它还将命令绑定到(kbd C-c C-s)

于 2013-02-16T18:02:39.060 回答