【发布时间】:2012-09-25 15:50:14
【问题描述】:
我注意到在长时间的编辑会话中,调用undo-tree (C-x C-u) 变得越来越慢。我认为原因是随着树变大,解析它需要更长的时间(因为它会跟踪我打开文件以来的所有编辑)
有什么方法可以清除打开文件上的撤消树?我试过C-x C-v (find-alternate-file),但这会重新打开重置光标的文件(在org-mode 等模式下会折叠我的列表)
【问题讨论】:
我注意到在长时间的编辑会话中,调用undo-tree (C-x C-u) 变得越来越慢。我认为原因是随着树变大,解析它需要更长的时间(因为它会跟踪我打开文件以来的所有编辑)
有什么方法可以清除打开文件上的撤消树?我试过C-x C-v (find-alternate-file),但这会重新打开重置光标的文件(在org-mode 等模式下会折叠我的列表)
【问题讨论】:
使用M-: (setq buffer-undo-tree nil) 重置buffer-undo-tree 变量。这将丢弃缓冲区的整个撤消历史记录,由 undo-tree.el 记录。
这也可以做成一个命令并绑定到一个键上,代码如下:
(defun clear-undo-tree ()
(interactive)
(setq buffer-undo-tree nil))
(global-set-key [(control c) u] 'clear-undo-tree)
【讨论】:
undo-tree-discard-history 尝试根据变量undo-limit、undo-strong-limit 和undo-outer-limit 修剪历史记录,而我的解决方案会删除整个撤消历史记录。更重要的是,undo-tree-discard-history 会在每个 undo/redo/switch-branch 操作上自动调用,所以显然它不适合你。
请注意,此提示在最近的撤消树版本中可能已过时。从 0.6 版开始,undo-tree 默认使用“惰性树绘制”。通过仅绘制可见部分(并在您在树周围移动时根据需要扩展它),这显着加快了大型撤消树的可视化。有关详细信息,请参阅undo-tree-visualizer-lazy-drawing 变量的文档字符串。
如果您仍然想丢弃整个撤消树,手动将buffer-undo-tree 设置为nil 是一个很好的一次性解决方案。一个长期的解决方案是减少 undo-limit、undo-strong-limit 和 undo-outer-limit 的值以限制撤消树的大小,方法是在丢弃旧数据之前限制它存储的撤消历史记录。
手动调用undo-tree-discard-history 毫无意义。每当您执行任何与撤消相关的操作时,它都会自动调用。
【讨论】:
有时我必须刷新缓冲区的撤消树,因为我粘贴了一些包含无法在缓冲区/文件的当前编码中显示的字符的文本。所以我发现@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)))))
【讨论】:
您可以修剪树而不是丢弃整个树:
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'.
【讨论】:
(undo-tree-discard-history)和将变量(buffer-undo-tree)设置为nil有什么区别? (如@user4815162342 回答)