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?
> 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
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.
> > 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/
>> 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