Is there any way to have Emacs save your undo history between sessions?

后端 未结 6 802
再見小時候
再見小時候 2020-12-28 13:57

Is there any way to have EMACS save your undo history between sessions?

I\'m aware of the savehist lib, the saveplace lib, the desktop lib, and the windows lib, thes

6条回答
  •  太阳男子
    2020-12-28 14:11

    Here's some code I wrote which seems to do the trick. It isn't bullet-proof, as in, it doesn't handle all the file handling intricacies that Emacs does (e.g. overriding where auto-save files are put, symlink handling, etc.). But, it seemed to do the trick for some simple text files I manipulated.

    (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)
    

提交回复
热议问题