Subject: bug#77580: [PATCH] New command ediff-undo

From: "Paul D. Nelson" <[email-masked]>
Date: Sat, 12 Apr 2025 23:15:26 +0200

Thanks so much for taking the time to think and offer feedback.

>> often that the last undo step is not what you think would be, and you
>> don't know how far you must look back

> We don't keep a count of buffer changes in our heads so that we could
> know how many changes back we need to go to get a diff that will
> contain the information we want.

The proposed commands can be useful when the number of undo steps is 1,
2 or infinity.  Examples:

1. After a buffer reverts because a file changes.

2. When you need to revert, perhaps just in a region, across file-save
boundaries without suitable VC or backup endpoints available, or (Juri's
example) across file and VC boundaries within a session.  (Good reason
to add the "minus prefix" option -- thanks Juri.)

3. After any complicated Emacs command.  Suppose you've just done a
query-replace-regexp with "!" that had unexpected effects.  As an
alternative to undoing and redoing the query-replace-regexp more
carefully, you can ediff-undo.  This is really a family of examples, one
for each complicated Emacs command.

In these cases, the ergonomics seem good to me.  For cases (1) and (2),
I'm not aware of an available alternative.

It's been pointed out that the proposed commands are unergonomic when
the prefix arg is large but not infinite.  In my view, there's nothing
intrinsically wrong with that.  By comparison, undo itself is not a
catch-all -- it's often better to use revert-buffer, backup files, VC,
or vundo.  We could steer the user away from such unergonomic cases by
prepending the new commands with

  (when (>= arg 3)
    (user-error "Find a better way"))

Jokes aside, can we improve the ergonomics in the "large but finite
argument" case without effectively reinventing vundo?  My first thought
(following Michael's "slave buffer" suggestion) is to consolidate the
two proposed commands into a single command, undo-preview, that displays
a read-only buffer *undo-preview* containing a clone of the current
buffer after ARG undo steps, with a transient keymap binding b, f, d, e,
q, and RET to undo, redo, diff, ediff, quit and apply.

Michael's more ambitious suggestion (pop-up views showing a visual
history) sounds excellent, but I don't have immediate thoughts on how to
achieve it, and wonder if it'd fit more naturally as a vundo
enhancement.  I've tried to keep the current proposal minimalist.