unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Undo complex command
@ 2021-03-23  7:05 scame
  2021-03-23 13:55 ` Stefan Monnier
  0 siblings, 1 reply; 5+ messages in thread
From: scame @ 2021-03-23  7:05 UTC (permalink / raw)
  To: emacs-devel@gnu.org

I use repeat complex command often (BTW, why doesn't the built in
version have completion? It's much better that way.) and
sometimes I want to undo them.

Imagine I replaced foo with bar in the whole buffer. I do various
other changes and then it occurs to me the foo/bar replacement
was a mistake and I want to undo it. These words occur many times
in the buffer, so it's not just a matter of selecting a region
and undo that.

What if after complex operations which change the buffer
significantly (e.g. touching many lines or something) emacs
stored the diff of the changes in memory? (With some limits,
of course, e.g. number of past diffs stored, size limit, etc.)

Then if I want to revert a particular change, I could say
M-x undo-complex-change, select a command from the undo
list and then I'd get its diff in a buffer and then I
could revert any hunk or the whole diff altogether. (Like
with regular diffs the diff can be applied as long as the
text did not change significantly since then.)

What do you think? Could it be a useful feature?




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

* Re: Undo complex command
  2021-03-23  7:05 Undo complex command scame
@ 2021-03-23 13:55 ` Stefan Monnier
  2021-03-29 23:09   ` Michael Heerdegen
  2021-03-31 15:37   ` scame
  0 siblings, 2 replies; 5+ messages in thread
From: Stefan Monnier @ 2021-03-23 13:55 UTC (permalink / raw)
  To: scame; +Cc: emacs-devel@gnu.org

> I use repeat complex command often (BTW, why doesn't the built in
> version have completion? It's much better that way.) and

It's always better with completion, indeed.  The reason why there is
currently no completion for it is simple: you just forgot to include the
patch for it in your message.
But don't worry, it happens to the best of us.  Just include it in your
next message and all will be forgiven.

> Then if I want to revert a particular change, I could say
> M-x undo-complex-change, select a command from the undo
> list and then I'd get its diff in a buffer and then I
> could revert any hunk or the whole diff altogether. (Like
> with regular diffs the diff can be applied as long as the
> text did not change significantly since then.)
>
> What do you think? Could it be a useful feature?

Sounds like a great idea, and it could be used with all commands (tho
you'd probably want to weed out the trivial `self-insert-command`s and
`delete-char`s).  I don't think you need to store the diff, because you
can (re)create the diff from the current undo info.

Here's how I imagine you could implement it:
1- copy the current `buffer-string` along with the `buffer-undo-list` to
   a new buffer.
2- save that buffer's content to a temp file.
3- call undo in that temp buffer
4- run a diff between the temp file and the current content of the temp buffer
5- display it to the user
6- if the user hits M-p (or something like that) you go back to 2.
7- else you try to apply that diff to the main buffer's contents
   (e.g. using diff-apply-hunk).

In step 1 you'll probably want to filter out all the marker updates
from the `buffer-undo-list`.  And at step 6 you'll probably want to
allow M-n.

Results would be better if the undo log included some information you
could show to the user such as the command name, but currently that's
not available.


        Stefan




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

* Re: Undo complex command
  2021-03-23 13:55 ` Stefan Monnier
@ 2021-03-29 23:09   ` Michael Heerdegen
  2021-03-31 15:37   ` scame
  1 sibling, 0 replies; 5+ messages in thread
From: Michael Heerdegen @ 2021-03-29 23:09 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: scame, emacs-devel

Stefan Monnier <monnier@iro.umontreal.ca> writes:

> 1- copy the current `buffer-string` along with the `buffer-undo-list` to
>    a new buffer.
> 2- save that buffer's content to a temp file.
> 3- call undo in that temp buffer
> 4- run a diff between the temp file and the current content of the temp buffer
> 5- display it to the user
> 6- if the user hits M-p (or something like that) you go back to 2.
> 7- else you try to apply that diff to the main buffer's contents
>    (e.g. using diff-apply-hunk).

"undo-tree.el" has implemented more or less that.  It uses it to provide
a way to display diffs for each change in the visualizer.  I don't
recall if has a separate command to diff with the current buffer state.

Michael.



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

* Re: Undo complex command
  2021-03-23 13:55 ` Stefan Monnier
  2021-03-29 23:09   ` Michael Heerdegen
@ 2021-03-31 15:37   ` scame
  2021-04-02  3:22     ` Stefan Monnier
  1 sibling, 1 reply; 5+ messages in thread
From: scame @ 2021-03-31 15:37 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: emacs-devel@gnu.org

>
> Sounds like a great idea, and it could be used with all commands (tho
> you'd probably want to weed out the trivial `self-insert-command`s and
> `delete-char`s).

An implementation of the described feature was posted to reddit yesterday:

https://www.reddit.com/r/emacs/comments/mgkbsk/complex_undo_local_history_gif_demo_inside/





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

* Re: Undo complex command
  2021-03-31 15:37   ` scame
@ 2021-04-02  3:22     ` Stefan Monnier
  0 siblings, 0 replies; 5+ messages in thread
From: Stefan Monnier @ 2021-04-02  3:22 UTC (permalink / raw)
  To: scame; +Cc: emacs-devel@gnu.org

>> Sounds like a great idea, and it could be used with all commands (tho
>> you'd probably want to weed out the trivial `self-insert-command`s and
>> `delete-char`s).
>
> An implementation of the described feature was posted to reddit yesterday:
>
> https://www.reddit.com/r/emacs/comments/mgkbsk/complex_undo_local_history_gif_demo_inside/

IIUC it's a popular place to advertise a package, but I've never seen it
recommended it as a place to distribute a package.
Is the code available at a more ... normal place?


        Stefan




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

end of thread, other threads:[~2021-04-02  3:22 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-23  7:05 Undo complex command scame
2021-03-23 13:55 ` Stefan Monnier
2021-03-29 23:09   ` Michael Heerdegen
2021-03-31 15:37   ` scame
2021-04-02  3:22     ` 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).