[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/undo-tree 2947d7c 169/195: Add hooks to transform/disca
From: |
Stefan Monnier |
Subject: |
[elpa] externals/undo-tree 2947d7c 169/195: Add hooks to transform/discard undo elements on saving/loading. |
Date: |
Sat, 28 Nov 2020 13:41:46 -0500 (EST) |
branch: externals/undo-tree
commit 2947d7cd68c279598a51bbc04fc59ac743f59253
Author: Toby S. Cubitt <toby-undo-tree@dr-qubit.org>
Commit: Toby S. Cubitt <toby-undo-tree@dr-qubit.org>
Add hooks to transform/discard undo elements on saving/loading.
---
undo-tree.el | 115 ++++++++++++++++++++++++++++++++++++++++++++---------------
1 file changed, 87 insertions(+), 28 deletions(-)
diff --git a/undo-tree.el b/undo-tree.el
index a434f61..54b5be7 100644
--- a/undo-tree.el
+++ b/undo-tree.el
@@ -1005,6 +1005,26 @@ enabled. However, this effect is quite rare in practice."
(integer :tag "> size")))
+(defvar undo-tree-pre-save-element-functions '()
+ "Special hook to modify undo-tree elements prior to saving.
+Each function on this hook is called in turn on each undo element
+in the tree by `undo-tree-save-history' prior to writing the undo
+history to file. It should return either nil, which removes that
+undo element from the saved history, or a replacement element to
+use instead (which should be identical to the original element if
+that element should be saved unchanged).")
+
+
+(defvar undo-tree-post-load-element-functions '()
+ "Special hook to modify undo-tree undo elements after loading.
+Each function on this hook is called in turn on each undo element
+in the tree by `undo-tree-load-history' after loading the undo
+history from file. It should return either nil, which removes that
+undo element from the loaded history, or a replacement element to
+use instead (which should be identical to the original element if
+that element should be loaded unchanged).")
+
+
(defface undo-tree-visualizer-default-face
'((((class color)) :foreground "gray"))
"Face used to draw undo-tree in visualizer."
@@ -1280,10 +1300,11 @@ in visualizer."
(size 0)
(count 0)
(object-pool (make-hash-table :test 'eq :weakness 'value))))
- ;;(:copier nil)
- )
+ (:copier nil))
root current size count object-pool)
+(defun copy-undo-tree (tree)
+ (copy-tree tree 'copy-vectors))
(defstruct
@@ -3100,29 +3121,41 @@ without asking for confirmation."
(when (or (not (file-exists-p filename))
overwrite
(yes-or-no-p (format "Overwrite \"%s\"? " filename)))
- (unwind-protect
- (progn
- ;; transform undo-tree into non-circular structure, and make
- ;; temporary copy
- (undo-tree-decircle buffer-undo-tree)
- (setq tree (copy-undo-tree buffer-undo-tree))
- ;; 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, 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))))
- ;; restore circular undo-tree data structure
- (undo-tree-recircle buffer-undo-tree))
- ))))
+ ;; transform undo-tree into non-circular structure, and make temporary
+ ;; copy
+ (setq tree (copy-undo-tree buffer-undo-tree))
+ (undo-tree-decircle tree)
+ ;; discard undo-tree object pool before saving
+ (setf (undo-tree-object-pool tree) nil)
+ ;; run pre-save transformer functions
+ (when undo-tree-pre-save-element-functions
+ (undo-tree-mapc
+ (lambda (node)
+ (let ((changeset (undo-tree-node-undo node)))
+ (run-hook-wrapped
+ 'undo-tree-pre-save-element-functions
+ (lambda (fun)
+ (setq changeset (delq nil (mapcar fun changeset)))))
+ (setf (undo-tree-node-undo node) changeset))
+ (let ((changeset (undo-tree-node-redo node)))
+ (run-hook-wrapped
+ 'undo-tree-pre-save-element-functions
+ (lambda (fun)
+ (setq changeset (delq nil (mapcar fun changeset)))))
+ (setf (undo-tree-node-redo node) changeset)))
+ (undo-tree-root tree)))
+ ;; print undo-tree to file
+ ;; 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)))))))
@@ -3178,7 +3211,23 @@ signaling an error if file is not found."
"Error reading undo-tree history from \"%s\"" filename)
(throw 'load-error nil)))
(kill-buffer nil)))
- ;; initialise empty undo-tree object pool
+ ;; run post-load transformer functions
+ (when undo-tree-post-load-element-functions
+ (undo-tree-mapc
+ (lambda (node)
+ (let ((changeset (undo-tree-node-undo node)))
+ (run-hook-wrapped
+ 'undo-tree-post-load-element-functions
+ (lambda (fun)
+ (setq changeset (delq nil (mapcar fun changeset)))))
+ (setf (undo-tree-node-undo node) changeset))
+ (let ((changeset (undo-tree-node-redo node)))
+ (run-hook-wrapped
+ 'undo-tree-post-load-element-functions
+ (lambda (fun)
+ (setq changeset (delq nil (mapcar fun changeset)))))
+ (setf (undo-tree-node-redo node) changeset)))
+ (undo-tree-root tree))) ;; initialise empty undo-tree object
pool
(setf (undo-tree-object-pool tree)
(make-hash-table :test 'eq :weakness 'value))
;; restore circular undo-tree data structure
@@ -3188,17 +3237,27 @@ signaling an error if file is not found."
;; Versions of save/load functions for use in hooks
-(defun undo-tree-save-history-hook ()
+(defun undo-tree-save-history-from-hook ()
(when (and undo-tree-mode undo-tree-auto-save-history
(not (eq buffer-undo-list t)))
(undo-tree-save-history nil t) nil))
-(defun undo-tree-load-history-hook ()
+(define-obsolete-function-alias
+ 'undo-tree-save-history-hook 'undo-tree-save-history-from-hook
+ "`undo-tree-save-history-hook' is obsolete since undo-tree
+ version 0.6.6. Use `undo-tree-save-history-from-hook' instead.")
+
+(defun undo-tree-load-history-from-hook ()
(when (and undo-tree-mode undo-tree-auto-save-history
(not (eq buffer-undo-list t))
(not revert-buffer-in-progress-p))
(undo-tree-load-history nil t)))
+(define-obsolete-function-alias
+ 'undo-tree-load-history-hook 'undo-tree-load-history-from-hook
+ "`undo-tree-load-history-hook' is obsolete since undo-tree
+ version 0.6.6. Use `undo-tree-load-history-from-hook' instead.")
+
- [elpa] externals/undo-tree ffb346a 157/195: Install undo-tree undo/redo Edit menu bar items., (continued)
- [elpa] externals/undo-tree ffb346a 157/195: Install undo-tree undo/redo Edit menu bar items., Stefan Monnier, 2020/11/28
- [elpa] externals/undo-tree 19baf49 158/195: Fix undo/redo-in-region tree diagrams in Commentary., Stefan Monnier, 2020/11/28
- [elpa] externals/undo-tree 22d6c01 153/195: Fix bug that cleared tree when enabling undo-tree-mode., Stefan Monnier, 2020/11/28
- [elpa] externals/undo-tree 46639dd 154/195: Tweak undo history file names to match Emacs backup file names., Stefan Monnier, 2020/11/28
- [elpa] externals/undo-tree fba51e6 146/195: Add undo-tree-visualizer[-mouse]-select functions to select node at pos or click., Stefan Monnier, 2020/11/28
- [elpa] externals/undo-tree 43663f6 159/195: Fix bug in menu entries that triggered error on null buffer-undo-tree., Stefan Monnier, 2020/11/28
- [elpa] externals/undo-tree 133b439 148/195: Set protected-local property on various visualizer variables., Stefan Monnier, 2020/11/28
- [elpa] externals/undo-tree bc9d095 156/195: Clear undo-tree-visualizer-needs-extending-[up|down] before drawing tree., Stefan Monnier, 2020/11/28
- [elpa] externals/undo-tree b35a6af 160/195: Bump copyright year and version number., Stefan Monnier, 2020/11/28
- [elpa] externals/undo-tree a3e81b6 161/195: Fix bug that caused undo-tree to hang when undoing in region (bug#16377)., Stefan Monnier, 2020/11/28
- [elpa] externals/undo-tree 2947d7c 169/195: Add hooks to transform/discard undo elements on saving/loading.,
Stefan Monnier <=
- [elpa] externals/undo-tree c3d04ea 165/195: Change obsolete subtract-time -> time-subtract., Stefan Monnier, 2020/11/28
- [elpa] externals/undo-tree af99ee3 166/195: Uncoditionally clear visualizer data before writing to file., Stefan Monnier, 2020/11/28
- [elpa] externals/undo-tree f6471ef 173/195: Simplify undo-list-byte-size., Stefan Monnier, 2020/11/28
- [elpa] externals/undo-tree ea165ed 174/195: Refactor undo-list-transfer-to-tree to not act directly on buffer-undo-list., Stefan Monnier, 2020/11/28
- [elpa] externals/undo-tree 8842bb5 176/195: More undo-tree GC corruption mitigations., Stefan Monnier, 2020/11/28
- [elpa] externals/undo-tree f6f557c 178/195: Fix various bugs in undo history loading/saving., Stefan Monnier, 2020/11/28
- [elpa] externals/undo-tree b8652b4 180/195: Null undo-tree-limit attemps to preserve all undo history., Stefan Monnier, 2020/11/28
- [elpa] externals/undo-tree f0a6192 184/195: Proper fix to copy-undo-tree by writing bespoke copier., Stefan Monnier, 2020/11/28
- [elpa] externals/undo-tree 3090c4f 185/195: Temporarily increase max-lisp-eval-depth and max-specpdl-size when copying undo-trees., Stefan Monnier, 2020/11/28
- [elpa] externals/undo-tree 8cf384f 187/195: Redraw visualizer when history-discarding invalidates it., Stefan Monnier, 2020/11/28