Emacs:清理撤消树

时间:2012-10-04 14:59:24

标签: emacs undo

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

有没有办法在打开的文件中清除undos树?我已尝试C-x C-vfind-alternate-file),但这会重新打开重置光标的文件(在org-mode之类的模式下会折叠我的列表)

4 个答案:

答案 0 :(得分:6)

使用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)

答案 1 :(得分:4)

请注意,此提示可能在最近的撤消树版本中已过时。从版本0.6开始,undo-tree默认使用“lazy tree-drawing”。通过仅绘制可见部分(并在树中移动时根据需要进行扩展),这可以显着加快大型撤消树的可视化速度。有关详细信息,请参阅undo-tree-visualizer-lazy-drawing变量的docstring。

如果您仍想放弃整个撤消树,请手动将buffer-undo-tree设置为nil是一个很好的一次性解决方案。较长期的解决方案是通过限制丢弃旧数据之前存储的撤消历史记录数来减少undo-limitundo-strong-limitundo-outer-limit的值以限制撤消树的大小。 / p>

手动呼叫undo-tree-discard-history毫无意义。每当你做任何撤消相关的事情时,它都会被自动调用。

答案 2 :(得分: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'.

答案 3 :(得分:1)

偶尔我必须刷新缓冲区的undo-tree,因为我粘贴了一些包含无法在缓冲区/文件当前编码中显示的字符的文本。所以我发现@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)))))