unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Support for undo-amalgamate in a version of the atomic-change-group macro (with patch)
@ 2021-10-29  1:48 Campbell Barton
  2021-11-01 17:43 ` Stefan Monnier
  0 siblings, 1 reply; 13+ messages in thread
From: Campbell Barton @ 2021-10-29  1:48 UTC (permalink / raw)
  To: Emacs developers

[-- Attachment #1: Type: text/plain, Size: 569 bytes --]

Hi, there have been a handful of times I've needed to treat multiple
actions as a single action,
others too (see stackoverflow question [0]).

While this is supported using change groups, the resulting macro ends
up being nearly identical to the existing `atomic-change-group' macro.
The only difference is a call to undo-amalgamate-change-group.

Attached a patch to add `atomic-change-group-undo-amalgamate' so
packages don't need to use a modified copy of this macro.

[0]: https://emacs.stackexchange.com/questions/7558/how-to-collapse-undo-history

-- 
- Campbell

[-- Attachment #2: emacs-atomic-change-group-undo-amalgamate.diff --]
[-- Type: text/x-patch, Size: 2306 bytes --]

commit 5cf79a19dac5187b6e8f4f6c3798d0f348eff89c
Author: Campbell Barton <ideasman42@gmail.com>
Date:   Fri Oct 29 12:33:29 2021 +1100

    Add a version of atomic-change-group that amalgamates undo steps

diff --git a/lisp/subr.el b/lisp/subr.el
index 39676249cd..4d2ca0b953 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -3490,15 +3490,8 @@ y-or-n-p
 \f
 ;;; Atomic change groups.
 
-(defmacro atomic-change-group (&rest body)
-  "Like `progn' but perform BODY as an atomic change group.
-This means that if BODY exits abnormally,
-all of its changes to the current buffer are undone.
-This works regardless of whether undo is enabled in the buffer.
-
-This mechanism is transparent to ordinary use of undo;
-if undo is enabled in the buffer and BODY succeeds, the
-user can undo the change normally."
+(defmacro atomic-change-group-1 (undo-amalgamate &rest body)
+  "Implementation of `atomic-change-group' & `atomic-change-group-amalgamate'."
   (declare (indent 0) (debug t))
   (let ((handle (make-symbol "--change-group-handle--"))
 	(success (make-symbol "--change-group-success--")))
@@ -3519,9 +3512,28 @@ atomic-change-group
 	 ;; Either of these functions will disable undo
 	 ;; if it was disabled before.
 	 (if ,success
-	     (accept-change-group ,handle)
+             (progn
+	       (accept-change-group ,handle)
+               (when ,undo-amalgamate
+                 (undo-amalgamate-change-group ,handle)))
 	   (cancel-change-group ,handle))))))
 
+(defmacro atomic-change-group (&rest body)
+  "Like `progn' but perform BODY as an atomic change group.
+This means that if BODY exits abnormally,
+all of its changes to the current buffer are undone.
+This works regardless of whether undo is enabled in the buffer.
+
+This mechanism is transparent to ordinary use of undo;
+if undo is enabled in the buffer and BODY succeeds, the
+user can undo the change normally."
+  `(atomic-change-group-1 nil ,@body))
+
+(defmacro atomic-change-group-undo-amalgamate (&rest body)
+  "A version of  `atomic-change-group' that amalgamates
+the change groups undo data."
+  `(atomic-change-group-1 t ,@body))
+
 (defun prepare-change-group (&optional buffer)
   "Return a handle for the current buffer's state, for a change group.
 If you specify BUFFER, make a handle for BUFFER's state instead.

^ permalink raw reply related	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2021-11-08 13:40 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-29  1:48 Support for undo-amalgamate in a version of the atomic-change-group macro (with patch) Campbell Barton
2021-11-01 17:43 ` Stefan Monnier
2021-11-02  1:04   ` Campbell Barton
2021-11-02  2:14     ` Stefan Monnier
2021-11-02  2:14     ` Campbell Barton
2021-11-07  8:45       ` Campbell Barton
2021-11-07 13:21         ` Stefan Monnier
2021-11-07 23:09           ` Campbell Barton
2021-11-07 23:29             ` Campbell Barton
2021-11-08  4:39             ` Stefan Monnier
2021-11-08  4:46               ` Lars Ingebrigtsen
2021-11-08  5:41                 ` Campbell Barton
2021-11-08 13:40                   ` Stefan Monnier

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).