unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Stefan Monnier <monnier@iro.umontreal.ca>
To: Campbell Barton <ideasman42@gmail.com>
Cc: Lars Ingebrigtsen <larsi@gnus.org>,
	Emacs developers <emacs-devel@gnu.org>
Subject: Re: Support for undo-amalgamate in a version of the atomic-change-group macro (with patch)
Date: Mon, 08 Nov 2021 08:40:12 -0500	[thread overview]
Message-ID: <jwv8rxy4vnt.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: <CAEcf3NygCjigXK6Suovv8TW4KwgMa6edqn5trJ+quy6b6VOoOg@mail.gmail.com> (Campbell Barton's message of "Mon, 8 Nov 2021 16:41:48 +1100")

> Updated the patch, for what it's worth - I'm not so keen on the name
> `with-single-undo'.

Fair enough.  Besides, I believe it would be slightly misleading because
it doesn't guarantee that this makes exactly one undo step (it might be
include changes performed before/after).

I pushed your change as below.

Please note the changes in the commit message format to follow our
conventions, and the documentation that should ideally come with
such patches.


        Stefan


commit 5861b8d027382ecbd4c0d3dffc283b8ac95b5692
Author: Campbell Barton <ideasman42@gmail.com>
Date:   Mon Nov 8 16:33:39 2021 +1100

    * lisp/subr.el (with-undo-amalgamate): New macro
    
    This allows commands to be made without adding undo-barriers, e.g.
    kmacro-exec-ring-item.

diff --git a/doc/lispref/text.texi b/doc/lispref/text.texi
index fa1135b8026..937680c200d 100644
--- a/doc/lispref/text.texi
+++ b/doc/lispref/text.texi
@@ -1506,6 +1506,11 @@ Undo
 This function does not bind @code{undo-in-progress}.
 @end defun
 
+@defmac with-undo-amalgamate body@dots{}
+This macro removes all the undo boundaries inserted during the
+execution of @var{body} so that it can be undone as a single step.
+@end defmac
+
 Some commands leave the region active after execution in such a way that
 it interferes with selective undo of that command.  To make @code{undo}
 ignore the active region when invoked immediately after such a command,
diff --git a/etc/NEWS b/etc/NEWS
index a297948a11a..874af33c752 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -598,6 +598,10 @@ Use 'exif-parse-file' and 'exif-field' instead.
 \f
 * Lisp Changes in Emacs 29.1
 
++++
+*** New macro 'with-undo-amalgamate'
+It records a particular sequence of operations as a single undo step
+
 +++
 *** New command 'yank-media'.
 This command supports yanking non-plain-text media like images and
diff --git a/lisp/subr.el b/lisp/subr.el
index f6dbd00532e..5a5842d4287 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -3542,6 +3542,29 @@ atomic-change-group
 	     (accept-change-group ,handle)
 	   (cancel-change-group ,handle))))))
 
+(defmacro with-undo-amalgamate (&rest body)
+  "Like `progn' but perform BODY with amalgamated undo barriers.
+
+This allows multiple operations to be undone in a single step.
+When undo is disabled this behaves like `progn'."
+  (declare (indent 0) (debug t))
+  (let ((handle (make-symbol "--change-group-handle--")))
+    `(let ((,handle (prepare-change-group))
+           ;; Don't truncate any undo data in the middle of this,
+           ;; otherwise Emacs might truncate part of the resulting
+           ;; undo step: we want to mimic the behavior we'd get if the
+           ;; undo-boundaries were never added in the first place.
+           (undo-outer-limit nil)
+           (undo-limit most-positive-fixnum)
+           (undo-strong-limit most-positive-fixnum))
+       (unwind-protect
+           (progn
+             (activate-change-group ,handle)
+             ,@body)
+         (progn
+           (accept-change-group ,handle)
+           (undo-amalgamate-change-group ,handle))))))
+
 (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-11-08 13:40 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]

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=jwv8rxy4vnt.fsf-monnier+emacs@gnu.org \
    --to=monnier@iro.umontreal.ca \
    --cc=emacs-devel@gnu.org \
    --cc=ideasman42@gmail.com \
    --cc=larsi@gnus.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).