unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Campbell Barton <ideasman42@gmail.com>
To: Emacs developers <emacs-devel@gnu.org>
Subject: Support for undo-amalgamate in a version of the atomic-change-group macro (with patch)
Date: Fri, 29 Oct 2021 12:48:24 +1100	[thread overview]
Message-ID: <CAEcf3NwgG2BY_0m2ui7WtiDgcVx68CX0REv4MUSQDRQXohF6qA@mail.gmail.com> (raw)

[-- 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.

             reply	other threads:[~2021-10-29  1:48 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-29  1:48 Campbell Barton [this message]
2021-11-01 17:43 ` Support for undo-amalgamate in a version of the atomic-change-group macro (with patch) 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAEcf3NwgG2BY_0m2ui7WtiDgcVx68CX0REv4MUSQDRQXohF6qA@mail.gmail.com \
    --to=ideasman42@gmail.com \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).