8

我注意到在长时间的编辑会话中,调用undo-tree(Cx Cu) 变得越来越慢。我认为原因是随着树变大,解析它需要更长的时间(因为它会跟踪我打开文件以来的所有编辑)

有什么方法可以清除打开文件上的撤消树?我已经尝试过C-x C-vfind-alternate-file),但这会重新打开重置光标的文件(并且在org-mode 折叠我的列表等模式下)

4

4 回答 4

9

使用重置buffer-undo-tree变量M-: (setq buffer-undo-tree nil)。这将丢弃缓冲区的整个撤消历史记录,如undo-tree.el.

这也可以做成一个命令并绑定到一个键上,代码如下:

(defun clear-undo-tree ()
  (interactive)
  (setq buffer-undo-tree nil))
(global-set-key [(control c) u] 'clear-undo-tree)
于 2012-10-04T20:02:37.277 回答
4

请注意,此提示在最近的撤消树版本中可能已过时。从 0.6 版开始,undo-tree 默认使用“惰性树绘制”。通过仅绘制可见部分(并在您在树周围移动时根据需要扩展它),这显着加快了大型撤消树的可视化。有关更多详细信息,请参阅undo-tree-visualizer-lazy-drawing变量的文档字符串。

如果您仍然想丢弃整个撤消树,手动设置buffer-undo-treenil是一个很好的一次性解决方案。一个长期的解决方案是通过限制在丢弃旧数据之前存储多少撤消历史记录来减小 、 和 的值并限制撤消树的undo-limit大小undo-strong-limitundo-outer-limit

手动调用undo-tree-discard-history是没有意义的。每当您执行任何与撤消相关的操作时,它都会自动调用。

于 2012-12-04T19:47:30.537 回答
2

有时我必须刷新缓冲区的撤消树,因为我粘贴了一些包含无法在缓冲区/文件的当前编码中显示的字符的文本。所以我发现@Amelio Vazquez-Reina 的功能很有帮助。但是,我不希望函数被意外调用,所以添加了一个确认提示和一个buffer-undo-tree确实是缓冲区局部变量的检查:

(defun clean-undo-tree ()
"Clear current buffer's undo-tree.
Undo-tree can cause problems with file encoding when characters
are inserted that cannot be represented using the files current
encoding. This is even the case when characters are only
temporarily inserted, e.g. pasted from another source and then
instantly deleted. In these situations it can be necessary to
clear the buffers undo-tree before saving the file."
  (interactive)
  (let ((buff (current-buffer)))
    (if (local-variable-p 'buffer-undo-tree)
        (if (y-or-n-p "Clear buffer-undo-tree? ")
            (progn
              (setq buffer-undo-tree nil)
              (message "Cleared undo-tree of buffer: %s" (buffer-name buff)))
          (message "Cancelled clearing undo-tree of buffer: %s" (buffer-name buff)))
      (error "Buffer %s has no local binding of `buffer-undo-tree'" (buffer-name buff)))))
于 2017-12-03T22:54:12.783 回答
1

您可以修剪树而不是丢弃整个东西:

undo-tree-discard-history is a compiled Lisp function in
`undo-tree.el'.

(undo-tree-discard-history)

Discard undo history until we're within memory usage limits
set by `undo-limit', `undo-strong-limit' and `undo-outer-limit'.
于 2012-10-04T22:05:17.017 回答