commit 76b39862354b2313cd5612adceca33ea60f3f4c6 parent 1aa13cff59d04f43bd5f4e3a1ba276527c52888e Author: Wojciech Siewierski <wojciech@siewierski.eu> Date: Sun, 5 Mar 2023 16:06:10 +0100 Allow kmacro-x-mc-atomic-undo-mode to also undo the original change Note: Usually any such change-group operations are wrapped in unwind-protect. I don't think this is necessary here, as we use the minor-mode (de)activation logic to first activate the change-group on mode enabling and then to accept-change-group it on mode disabling. No errors in-between them should affect the fact we'll disable the mode at some point and the cleanup will happen. This is different from doing it within a single function which may be interrupted with an error. https://www.gnu.org/software/emacs/manual/html_node/elisp/Atomic-Changes.html Diffstat:
| M | kmacro-x-mc.el | | | 26 | ++++++++++++++++++-------- |
1 file changed, 18 insertions(+), 8 deletions(-)
diff --git a/kmacro-x-mc.el b/kmacro-x-mc.el @@ -276,20 +276,30 @@ user directory. (kill-local-variable 'kmacro-x-mc-offsets) (kill-local-variable 'kmacro-x-mc-cursors))) + +(defvar-local kmacro-x-mc-change-group nil) + +(defun kmacro-x-mc-undo-amalgamate-advice (&rest _) + "Amalgamate all the changes created with `kmacro-x-mc-mode' active." + (if kmacro-x-mc-mode + (progn + (undo-boundary) + (setq-local kmacro-x-mc-change-group (prepare-change-group)) + (activate-change-group kmacro-x-mc-change-group)) + (undo-amalgamate-change-group kmacro-x-mc-change-group) + (accept-change-group kmacro-x-mc-change-group) + (kill-local-variable 'kmacro-x-mc-change-group))) + ;;;###autoload (define-minor-mode kmacro-x-mc-atomic-undo-mode - "Undo the whole `kmacro-x-mc-mode' bulk operation at once. - -Note: The original actions used for the macro recording are not -undone, only the the macro executions (called from -`kmacro-x-mc-apply')." + "Undo the whole `kmacro-x-mc-mode' bulk operation at once." :global t :require 'kmacro-x-mc (if kmacro-x-mc-atomic-undo-mode - (advice-add #'kmacro-x-mc-apply :around - #'kmacro-x-undo-amalgamate-advice) + (advice-add #'kmacro-x-mc-mode :after + #'kmacro-x-mc-undo-amalgamate-advice) (advice-remove #'kmacro-x-mc-apply - #'kmacro-x-undo-amalgamate-advice))) + #'kmacro-x-mc-undo-amalgamate-advice))) (provide 'kmacro-x-mc) ;;; kmacro-x-mc.el ends here