emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/undo-tree 4c14173 115/195: Fix undo history auto-saving


From: Stefan Monnier
Subject: [elpa] externals/undo-tree 4c14173 115/195: Fix undo history auto-saving.
Date: Sat, 28 Nov 2020 13:41:34 -0500 (EST)

branch: externals/undo-tree
commit 4c14173fe380e6f157c8a58b4ce73f37b0e16630
Author: Toby S. Cubitt <toby-undo-tree@dr-qubit.org>
Commit: Toby S. Cubitt <toby-undo-tree@dr-qubit.org>

    Fix undo history auto-saving.
    
    History auto-save hooks must be installed globally, otherwise
    undo-tree-load-history-hook doesn't run because file has already been loaded
    before `undo-tree-mode' has installed the hook function.
    
    Ensure auto-compression-mode is enabled when saving undo history to file, so
    that undo history is always compressed if
    undo-tree-make-history-save-file-name has been overridden or advised to add 
a
    compressed filename extension.
    
    Document persistent undo tree storage in Commentary.
---
 undo-tree.el | 92 +++++++++++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 66 insertions(+), 26 deletions(-)

diff --git a/undo-tree.el b/undo-tree.el
index 411e554..3a5cedf 100644
--- a/undo-tree.el
+++ b/undo-tree.el
@@ -196,6 +196,39 @@
 ;;
 ;;
 ;;
+;; Persistent undo history:
+;;
+;; Note: Requires a recent development version of Emacs checked out out from
+;;       the Emacs bzr repository. All stable versions of Emacs currently
+;;       break this feature.
+;;
+;; `undo-tree-auto-save-history' (variable)
+;;    automatically save and restore undo-tree history along with buffer
+;;    (disabled by default)
+;;
+;; `undo-tree-save-history' (command)
+;;    manually save undo history to file
+;;
+;; `undo-tree-load-history' (command)
+;;    manually load undo history from file
+;;
+;;
+;;
+;; Compressing undo history:
+;;
+;;   Undo history files cannot grow beyond the maximum undo tree size, which
+;;   is limited by `undo-limit', `undo-strong-limit' and
+;;   `undo-outer-limit'. Nevertheless, undo history files can grow quite
+;;   large. If you want to automatically compress undo history, add the
+;;   following advice to your .emacs file (replacing ".gz" with the filename
+;;   extension of your favourite compression algorithm):
+;;
+;;   (defadvice undo-tree-make-history-save-file-name
+;;     (after undo-tree activate)
+;;     (setq concat ad-return-value ".gz"))
+;;
+;;
+;;
 ;;
 ;; Undo Systems
 ;; ============
@@ -691,10 +724,12 @@
 ;;; Change Log:
 ;;
 ;; Version 0.5.4
-;; * use `with-temp-buffer' instead of `with-temp-file' when saving undo
-;;   history, so that `auto-compression-mode' can take effect if
-;;   `undo-tree-make-history-save-file-name' has been advised in the user's
+;; * support compression of undo history files if
+;;   `undo-tree-make-history-save-file-name' has been redefined or advised in
 ;;   .emacs to return a compressed filename extension
+;; * install history auto-save hooks globally, otherwise
+;;   `undo-tree-load-history-hook' doesn't run because file has already been
+;;   loaded before `undo-tree-mode' has installed the hook function
 ;;
 ;; Version 0.5.3
 ;; * modified `undo-list-transfer-to-tree' and `undo-list-pop-changeset' to
@@ -1162,6 +1197,15 @@ in visualizer."
 
 
 ;;; =================================================================
+;;;                 Install history-auto-save hooks
+
+(add-hook 'write-file-functions 'undo-tree-save-history-hook)
+(add-hook 'find-file-hook 'undo-tree-load-history-hook)
+
+
+
+
+;;; =================================================================
 ;;;                     Setup default keymaps
 
 (defvar undo-tree-map nil
@@ -2623,21 +2667,11 @@ Within the undo-tree visualizer, the following keys are 
available:
   undo-tree-mode-lighter    ; lighter
   undo-tree-map             ; keymap
 
-  (cond
-   ;; if enabling `undo-tree-mode', set up history-saving hooks if
-   ;; `undo-tree-auto-save-history' is enabled
-   (undo-tree-mode
-    (when undo-tree-auto-save-history
-      (add-hook 'write-file-functions 'undo-tree-save-history-hook nil t)
-      (add-hook 'find-file-hook 'undo-tree-load-history-hook nil t)))
-   ;; if disabling `undo-tree-mode', rebuild `buffer-undo-list' from tree so
-   ;; Emacs undo can work
-   (t
+  ;; if disabling `undo-tree-mode', rebuild `buffer-undo-list' from tree so
+  ;; Emacs undo can work
+  (if (not undo-tree-mode)
     (undo-list-rebuild-from-tree)
-    (setq buffer-undo-tree nil)
-    (when undo-tree-auto-save-history
-      (remove-hook 'write-file-functions 'undo-tree-save-history-hook t)
-      (remove-hook 'find-file-hook 'undo-tree-load-history-hook t)))))
+    (setq buffer-undo-tree nil)))
 
 
 (defun turn-on-undo-tree-mode (&optional print-message)
@@ -3059,13 +3093,17 @@ without asking for confirmation."
        ;; discard undo-tree object pool before saving
        (setf (undo-tree-object-pool tree) nil)
        ;; print undo-tree to file
-       ;; NOTE: we use `with-temp-buffer' instead of `with-temp-file' to
-       ;;       allow `auto-compression-mode' to take effect
-       (with-temp-buffer
-         (prin1 (sha1 buff) (current-buffer))
-         (terpri (current-buffer))
-         (let ((print-circle t)) (prin1 tree (current-buffer)))
-         (write-region nil nil filename))))))
+       ;; NOTE: We use `with-temp-buffer' instead of `with-temp-file' to
+       ;;       allow `auto-compression-mode' to take effect, in case user
+       ;;       has overridden or advised the default
+       ;;       `undo-tree-make-history-save-file-name' to add a compressed
+       ;;       file extension.
+       (with-auto-compression-mode
+         (with-temp-buffer
+           (prin1 (sha1 buff) (current-buffer))
+           (terpri (current-buffer))
+           (let ((print-circle t)) (prin1 tree (current-buffer)))
+           (write-region nil nil filename)))))))
 
 
 
@@ -3127,10 +3165,12 @@ signaling an error if file is not found."
 
 ;; Versions of save/load functions for use in hooks
 (defun undo-tree-save-history-hook ()
-  (undo-tree-save-history nil t) nil)
+  (when (and undo-tree-mode undo-tree-auto-save-history)
+    (undo-tree-save-history nil t) nil))
 
 (defun undo-tree-load-history-hook ()
-  (undo-tree-load-history nil t))
+  (when (and undo-tree-mode undo-tree-auto-save-history)
+    (undo-tree-load-history nil t)))
 
 
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]