Sublime Forum

Supporting "undo" in multiple selections

#1

I’m writing a plugin which inserts many characters in all the selections (i.e. in several regions).
I’m using view.replace(region, "newstring") to replace each region with the new string.

My problem is, when someone hits “undo” (ctrl+z), it will undo the replacements one at a time, and not all at once.
This is contrary to the way it work when just writing text in multiple selections, where “undo” will undo all the written lines at once.

What’s the best way to make my plugin support “undo”?

Thanks,
Edan

0 Likes

#2

Funny, I just found out about this a few days ago. I don’t know if the behavior’s changed in the latest beta, or if I just never noticed.

Anyways, here are a few ideas:

  • Undo should undo everything the command just did, and Soft-undo should do it step by step. KISS!

  • Let the plugin decide, through the API, how to “atomicize” operations. Power to the people!

  • Nested undo? Per-region undo
    ? The holy grail of undos?

0 Likes

#3

The problem is, my plugin effectively “runs” the command “region.replace” several times, which is why Sublime treats it as several separate “undo” units.

I’d just add an api function which is the equivalent of SQL’s “BEGIN” command, which tells Sublime to treat several commands as one.

0 Likes

#4

This feature exists:
viewtopic.php?f=4&t=773&p=3319&hilit=undo+group&sid=5d3ceedc395657be60ac40ecd469d88d#p3319

0 Likes

#5

Nice! It works.

It should be added to the Commands reference, if only in the “Not Yet Documented” section :smile:

0 Likes

#6

Great! Thanks a lot.
I’m still working on making it work (it behaves very oddly part of the time), but it looks like this is exactly what I need.

0 Likes

#7

I’ve come across a problem with the commands.

It’s detailed in this thread, if anyone is interested: https://forum.sublimetext.com/t/bug-using-gluemarkedundogroups-from-a-quickpanel/806/1&sid=f53467780445306c08acdebd15f178b9&sid=232cae9cdc3f0f1639e1e279378f6cce#p5171

0 Likes

#8

It’s worth noting that in the general case, you don’t have to do anything to have this happen: any modifications to the buffer done within the run method of a TextCommand will automatically be grouped into a single undo action.

0 Likes

#9

Why isn’t it done in this case? Because it’s on the “onDone” method of a QuickPanel?
It might be worth making that also group the commands into a single action.

By the way, here’s a (possible) workaround: I can alway call a new “dummy” command from the onDone method (using runCommand()), and pass it the parameters somehow.
I’d just need to create a “dummy” command that does all the actual work.

0 Likes