mawww / kakoune

mawww's experiment for a better code editor
http://kakoune.org
The Unlicense
9.99k stars 715 forks source link

[REQUEST] Creating undo groups in Normal mode #5151

Closed ftonneau closed 6 months ago

ftonneau commented 7 months ago

Feature

In Insert mode, typing <c-u> commits "changes up to now as a single undo group" (quoting from :doc keys).

It would be great to have something similar in Normal mode, with specialized begin-undo-group and end-undo-group commands that would do exactly what they say.

Usecase

Some scripts/plugins run several changes behind the scene after one inserts a new character or word.

For example, I have a script for autowrapping that rewraps text automatically on insert, change, delete, paste, etc. The autowrapping commands are called from a hook and pipe the changed paragraph to a custom wrapper. The problem: if the user changes his/her mind and now types u in Normal mode, Kakoune will show the state of the screen before the piping (often with selections completely broken), not before the user's action. Seeing the state of the screen before the user's action requires typing u twice, which is annoying. Handling prose to make Kakoune behave more like a text processor would greatly benefit from begin-undo-group and end-undo-group script commands.

ftonneau commented 7 months ago

Let me add that the problem can be partly assuaged by pasting text into a scratch buffer, doing the changes in the scratch buffer, and pasting the modified content back into the original buffer (the scratch buffer is then deleted). This is better, but still not as good as begin-undo-group and end-undo-group commands would allow.

Screwtapello commented 7 months ago

If you put commands inside an execute-commands block, that effectively puts them in an undo group together.

This command produces two operations that can be undone individually:

kak -n -e 'exec x~; exec jx~'

This command produces one operation that can only be undone as a unit:

kak -n -e 'eval %{ exec x~; exec jx~; }'

This command also produces one operation that can only be undone as a unit:

kak -n -e 'exec x~jx~'
ftonneau commented 7 months ago

Great info! I just created a post in the users forum, in the Recipes & Guides category, as I thought the info you provided could be useful to people who do not necessarily check on GitHub.