emacs - 从文件还原缓冲区后,是否可以在Emacs中保留撤消列表?

标签 emacs elisp undo

在执行revert-buffer或使用auto-revert-mode之后,如何使Emacs为我的缓冲区保留其撤消历史记录?

在Vim中,如果在光盘上更改了在缓冲区中打开的文件,Vim会提示我重新加载该文件。然后,如果愿意,我可以简单地单击“u”来撤消重新加载,甚至从那时起再返回。当我恢复缓冲区时,Emacs似乎会浪费所有撤消信息。

最佳答案

我猜最明显的方法是杀死当前缓冲区内容,然后调用insert-file从文件中读取当前内容的函数。

如果对文件的更改包括对字符编码的更改,则可能有问题吗?我还没有测试过。

这是我目前的尝试。 IMO有点毛茸茸,但是还可以。

;; Allow buffer reverts to be undone
(defun my-revert-buffer (&optional ignore-auto noconfirm preserve-modes)
  "Revert buffer from file in an undo-able manner."
  (interactive)
  (when (buffer-file-name)
    ;; Based upon `delphi-save-state':
    ;; Ensure that any buffer modifications do not have any side
    ;; effects beyond the actual content changes.
    (let ((buffer-read-only nil)
          (inhibit-read-only t)
          (before-change-functions nil)
          (after-change-functions nil))
      (unwind-protect
          (progn
            ;; Prevent triggering `ask-user-about-supersession-threat'
            (set-visited-file-modtime)
            ;; Kill buffer contents and insert from associated file.
            (widen)
            (kill-region (point-min) (point-max))
            (insert-file-contents (buffer-file-name))
            ;; Mark buffer as unmodified.
            (set-buffer-modified-p nil))))))

(defadvice ask-user-about-supersession-threat
  (around my-supersession-revert-buffer)
  "Use my-revert-buffer in place of revert-buffer."
  (let ((real-revert-buffer (symbol-function 'revert-buffer)))
    (fset 'revert-buffer 'my-revert-buffer)
    ;; Note that `ask-user-about-supersession-threat' calls
    ;; (signal 'file-supersession ...), so we need to handle
    ;; the error in order to restore revert-buffer.
    (unwind-protect
        ad-do-it
      (fset 'revert-buffer real-revert-buffer))))

(ad-activate 'ask-user-about-supersession-threat)

令人讨厌的是,我只注意到revert-buffer文档中所有具有相关外观的信息,因此可能有一种更简单的方法来执行此操作。

If the value of revert-buffer-function is non-nil, it is called to do all the work for this command. Otherwise, the hooks before-revert-hook and after-revert-hook are run at the beginning and the end, and if revert-buffer-insert-file-contents-function is non-nil, it is called instead of rereading visited file contents.

关于emacs - 从文件还原缓冲区后,是否可以在Emacs中保留撤消列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4924389/

相关文章:

Emacs Lisp : how to avoid inserting a duplicate list item?

regex - Emacs - Lisp 中的正则表达式需要双重转义 - 为什么?

android - 实现撤消删除的更好方法

visual-studio-2008 - Visual Studio 2008 Emacs 模式

python - Emacs 无法创建与 raw.github.com 的连接(未找到 gzip)

elisp - 在 emacs lisp 中包装一个函数

language-agnostic - 撤消/重做与级联删除

python - 如何使用 Python/Django 实现 "undo"功能

emacs - 突出显示缓冲区修改

Emacs ediff 标记了不同目录缓冲区中的文件