有没有办法让Emacs在会话之间保存撤消历史记录?

时间:2010-06-06 17:09:30

标签: emacs

有没有办法让EMACS在会话之间保存撤消历史记录?

我知道savehist lib,saveplace lib,桌面库和windows lib,这些都提供了一些会话控制,但似乎都没有保存撤消历史记录。

6 个答案:

答案 0 :(得分:17)

从版本0.4开始,undo-tree支持在“开箱即用”的会话之间持久存储撤消树数据。 (请注意,在最新版本中有与此功能相关的重要错误修复;撰写本文时的最新版本为0.6.3。)

只需启用undo-tree-auto-save-history自定义选项,即可在撤消树缓冲区中自动保存和加载撤消历史记录。或者使用undo-tree-save/load-history命令手动保存和加载撤消历史记录。

至少需要Emacs版本24.3才能可靠地工作,但是使用最近的Emacs它可以很好地工作。 (在撰写本文时,这意味着从bzr获取Emacs的开发版本,并从源代码自行编译。但Emacs 24.3应该很快就会发布。)

答案 1 :(得分:12)

这里有一些我写的代码似乎可以解决这个问题。它不是防弹的,因为它不处理Emacs所做的所有文件处理复杂性(例如,覆盖放置自动保存文件的位置,符号链接处理等)。但是,它似乎为我操作的一些简单文本文件做了诀窍。

(defun save-undo-filename (orig-name)
  "given a filename return the file name in which to save the undo list"
  (concat (file-name-directory orig-name)
          "."
          (file-name-nondirectory orig-name)
          ".undo"))

(defun save-undo-list ()
  "Save the undo list to a file"
  (save-excursion
    (ignore-errors
      (let ((undo-to-save `(setq buffer-undo-list ',buffer-undo-list))
            (undo-file-name (save-undo-filename (buffer-file-name))))
        (find-file undo-file-name)
        (erase-buffer)
        (let (print-level
              print-length)
          (print undo-to-save (current-buffer)))
        (let ((write-file-hooks (remove 'save-undo-list write-file-hooks)))
          (save-buffer))
        (kill-buffer))))
  nil)

(defvar handling-undo-saving nil)

(defun load-undo-list ()
  "load the undo list if appropriate"
  (ignore-errors
    (when (and
           (not handling-undo-saving)
           (null buffer-undo-list)
           (file-exists-p (save-undo-filename (buffer-file-name))))
      (let* ((handling-undo-saving t)
             (undo-buffer-to-eval (find-file-noselect (save-undo-filename (buffer-file-name)))))
        (eval (read undo-buffer-to-eval))))))

(add-hook 'write-file-hooks 'save-undo-list)
(add-hook 'find-file-hook 'load-undo-list)

答案 2 :(得分:10)

将以下内容添加到.emacs文件中:

(global-undo-tree-mode)
(setq undo-tree-auto-save-history t)
(setq undo-tree-history-directory-alist '(("." . "~/.emacs.d/undo")))

解释

  • (global-undo-tree-mode)启用撤消树。

  • (setq undo-tree-auto-save-history t)可以自动保存撤消历史记录。

  • (setq undo-tree-history-directory-alist '(("." . "~/.emacs.d/undo")))这样您的项目就不会被破坏历史保存文件所困扰。

答案 3 :(得分:8)

桌面保存模式默认不保存buffer-undo-list。你只需要告诉他!

(add-to-list 'desktop-locals-to-save 'buffer-undo-list)

答案 4 :(得分:2)

Emacs Session似乎支持这一点:

(add-to-list 'session-locals-include 'buffer-undo-list)

答案 5 :(得分:0)

通过使用以下提供的信息,我设法使撤消历史记录起作用:http://emacs.stackexchange.com/q/3725/2287

我创建了一条建议,该建议临时覆盖desktop.el.gz,而不是修补原始文件(buffer-local-variables),然后将其与收集有关缓冲区信息的函数一起使用。

(defun +append-buffer-undo-list-to-buffer-local-variables-advice (orig-fn &rest args)
  "Override `buffer-local-variables' and call ORIG-FN with ARGS.
There is a bug in Emacs where the `buffer-undo-list' data is
missing from the output of `buffer-local-variables'. This
advice temporarily overrides the function and appends the
missing data."
  (let ((orig-buffer-local-variables-fn (symbol-function 'buffer-local-variables)))
    (cl-letf (((symbol-function 'buffer-local-variables)
               #'(lambda () (append (funcall orig-buffer-local-variables-fn)
                               `(,(cons 'buffer-undo-list buffer-undo-list))))))
      (apply orig-fn args))))

(advice-add #'desktop-buffer-info :around #'+append-buffer-undo-list-to-buffer-local-variables-advice)
(push 'buffer-undo-list desktop-locals-to-save)
(desktop-save-mode 1)

希望这对其他人有帮助。