* How to amalgamate changes across multiple buffers into a single undo entry
@ 2020-07-23 4:14 Brian Leung
2020-07-23 21:55 ` Michael Heerdegen
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Brian Leung @ 2020-07-23 4:14 UTC (permalink / raw)
To: help-gnu-emacs
Suppose one action causes a change in two buffers. I'd like a way of performing an undo operation so that both buffers will be undone at once. But I can't figure out how to amalgamate the buffer changes properly. This is what I've got:
(defmacro my/with-single-undo (curr-buf &rest body)
"Within CURR-BUF, execute BODY as a single undo step."
(declare (indent 1))
(let ((marker (cl-gensym "marker")))
`(let ((,marker
(nconc (prepare-change-group ,curr-buf)
(prepare-change-group (get-buffer-create "b.txt")))))
(unwind-protect
(progn ,@body)
(undo-amalgamate-change-group ,marker)))))
(defun my/insert-some-stuff (&rest _args)
(interactive)
(my/with-single-undo (current-buffer)
(insert "apple banana caterpillar\n")
(with-current-buffer (get-buffer-create "b.txt")
(insert "xylophone yak zebra"))))
I test by making "a.txt" and "b.txt" files, opening both up, and then executing M-x my/insert-some-stuff from within a.txt. Right now, undo is only undoing changes within the buffer in which I execute it.
I'd appreciate any help on this.
Best,
Brian
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: How to amalgamate changes across multiple buffers into a single undo entry
2020-07-23 4:14 How to amalgamate changes across multiple buffers into a single undo entry Brian Leung
@ 2020-07-23 21:55 ` Michael Heerdegen
2020-07-23 22:11 ` Michael Heerdegen
2020-07-24 1:44 ` Emanuel Berg via Users list for the GNU Emacs text editor
[not found] ` <mailman.353.1595490028.24492.help-gnu-emacs@gnu.org>
2 siblings, 1 reply; 5+ messages in thread
From: Michael Heerdegen @ 2020-07-23 21:55 UTC (permalink / raw)
To: help-gnu-emacs
Brian Leung <leungbk@mailfence.com> writes:
> Suppose one action causes a change in two buffers. I'd like a way of
> performing an undo operation so that both buffers will be undone at
> once. But I can't figure out how to amalgamate the buffer changes
> properly.
When I read the code it doesn't seem that this is supported. AFAIU,
`undo-amalgamate-change-group' handles all buffers of a multi-buffer
change group separately. I guess buffer-undo-list also just doesn't
support what you want.
Michael.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: How to amalgamate changes across multiple buffers into a single undo entry
2020-07-23 21:55 ` Michael Heerdegen
@ 2020-07-23 22:11 ` Michael Heerdegen
0 siblings, 0 replies; 5+ messages in thread
From: Michael Heerdegen @ 2020-07-23 22:11 UTC (permalink / raw)
To: help-gnu-emacs
Michael Heerdegen <michael_heerdegen@web.de> writes:
> I guess buffer-undo-list also just doesn't support what you want.
I think what you want is not well-defined. Say, after you inserted
something in A and something in B and make that operation a shared entry
in the undo history in both buffers, you change B and add something in
the middle of the inserted text so that it is split. Or you do a
complicated query-replace in the inserted text in B. You do other stuff
in A. Now you undo in A until your special entry is reached. What
would happen to B? What would it mean to the undo history of A when
that corresponding change in B can't be undone so easily?
Michael.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: How to amalgamate changes across multiple buffers into a single undo entry
2020-07-23 4:14 How to amalgamate changes across multiple buffers into a single undo entry Brian Leung
2020-07-23 21:55 ` Michael Heerdegen
@ 2020-07-24 1:44 ` Emanuel Berg via Users list for the GNU Emacs text editor
[not found] ` <mailman.353.1595490028.24492.help-gnu-emacs@gnu.org>
2 siblings, 0 replies; 5+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-07-24 1:44 UTC (permalink / raw)
To: help-gnu-emacs
Brian Leung wrote:
> Suppose one action causes a change in two buffers.
> I'd like a way of performing an undo operation so
> that both buffers will be undone at once [...]
Well, first, what do you mean exactly by "at once"?
You mean it should appear to the user as an atomic
event, right? (Or, correspondingly, if it is to be
invoked from Lisp, it should just require a single
invocation for both buffers?)
Well, yeah, I suppose that can be automated - just
iterate the buffers and `undo' in each and every one.
Like, for example, like this:
(require 'cl-lib)
(defun undo-in-two-buffers (msg bfr-a bfr-b)
(let ((buff-a (get-buffer-create bfr-a))
(buff-b (get-buffer-create bfr-b)) )
(cl-dolist (buff (list buff-a buff-b))
(with-current-buffer buff
(insert msg) ))
(cl-dolist (buff (list buff-a buff-b))
(with-current-buffer buff
(undo) ))))
;; (undo-in-two-buffers "undo me two times" "test-bfr-1" "test-bfr-2")
But... I don't like the idea of that use of `undo'.
Humans should use that as a part of the interactive
edit and muscle memory/finger habits. But it doesn't
feel right that program rely on it. Maybe I'm wrong
here, I don't know. In all my Elisp, I never used
`undo'.
--
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: How to amalgamate changes across multiple buffers into a single undo entry
[not found] ` <mailman.353.1595490028.24492.help-gnu-emacs@gnu.org>
@ 2020-07-26 10:30 ` Ihor Radchenko
0 siblings, 0 replies; 5+ messages in thread
From: Ihor Radchenko @ 2020-07-26 10:30 UTC (permalink / raw)
To: Brian Leung, help-gnu-emacs
> Suppose one action causes a change in two buffers. I'd like a way of performing an undo operation so that both buffers will be undone at once. But I can't figure out how to amalgamate the buffer changes properly. This is what I've got:
You might try to look into org-agenda-undo. It implements something
similar I think.
Best,
Ihor
Brian Leung <leungbk@mailfence.com> writes:
> Suppose one action causes a change in two buffers. I'd like a way of performing an undo operation so that both buffers will be undone at once. But I can't figure out how to amalgamate the buffer changes properly. This is what I've got:
>
> (defmacro my/with-single-undo (curr-buf &rest body)
> "Within CURR-BUF, execute BODY as a single undo step."
> (declare (indent 1))
> (let ((marker (cl-gensym "marker")))
> `(let ((,marker
> (nconc (prepare-change-group ,curr-buf)
> (prepare-change-group (get-buffer-create "b.txt")))))
> (unwind-protect
> (progn ,@body)
> (undo-amalgamate-change-group ,marker)))))
>
> (defun my/insert-some-stuff (&rest _args)
> (interactive)
> (my/with-single-undo (current-buffer)
> (insert "apple banana caterpillar\n")
> (with-current-buffer (get-buffer-create "b.txt")
> (insert "xylophone yak zebra"))))
>
> I test by making "a.txt" and "b.txt" files, opening both up, and then executing M-x my/insert-some-stuff from within a.txt. Right now, undo is only undoing changes within the buffer in which I execute it.
>
> I'd appreciate any help on this.
>
> Best,
> Brian
>
--
Ihor Radchenko,
PhD,
Center for Advancing Materials Performance from the Nanoscale (CAMP-nano)
State Key Laboratory for Mechanical Behavior of Materials, Xi'an Jiaotong University, Xi'an, China
Email: yantar92@gmail.com, ihor_radchenko@alumni.sutd.edu.sg
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2020-07-26 10:30 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-07-23 4:14 How to amalgamate changes across multiple buffers into a single undo entry Brian Leung
2020-07-23 21:55 ` Michael Heerdegen
2020-07-23 22:11 ` Michael Heerdegen
2020-07-24 1:44 ` Emanuel Berg via Users list for the GNU Emacs text editor
[not found] ` <mailman.353.1595490028.24492.help-gnu-emacs@gnu.org>
2020-07-26 10:30 ` Ihor Radchenko
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.