Sublime Forum

Undo, run_command and command_history

#1

Hello ST family,

I am absolutely new here, I would like to ask some question about plugin development regarding the undo feature:

  1. Is it possible to call ‘undo’ command in a TextCommand (or other Commands) to undo previous command (such as paste). I tried this but it just wouldn’t work:
import sublime, sublime_plugin

class MyUndoCommand(sublime_plugin.TextCommand):
	def run(self, edit):
		print 'I would like to undo last action (by other plugin, such as previous paste'
		self.view.run_command('undo')
  1. Is it possible to retrieve the command has been undo? It looks like that command_history just shift it out from the stack. Doing a search I found https://forum.sublimetext.com/t/detect-its-whether-a-undo-or-a-redo-command-in-on-modified/6235/1&hilit=undo#p33227 which mention glue_marked_undo_groups,unmark_undo_groups_for_gluing, maybe_mark_undo_groups_for_gluing but I can’t find document about them.

Thank you and happy STing.

0 Likes

#2

You can use a positive index for the redo stack:

Index 0 corresponds to the most recent command, -1 the command before that, and so on. Positive values for index indicate to look in the redo stack for commands. If the undo / redo history doesn't extend far enough, then (None, None, 0) will be returned. 
0 Likes

#3

Thank you, @bizoo, a big bisou for you :smile:

BTW, do you have any idea about the first question? WindowCommand seem allow us to create several undo points as you mentioned here https://forum.sublimetext.com/t/partial-undo/7993/1&hilit=undo#p40090, will it has something to do with undo too?

0 Likes

#4

No, don’t know why it doesn’t works.
Running view.run_command(‘undo’) from the console works as expected.

However, it looks like if you convert your TextCommand in WindowCommand, it works:

[code]import sublime, sublime_plugin

class MyUndoCommand(sublime_plugin.WindowCommand):
def run(self):
print ‘I would like to undo last action (by other plugin, such as previous paste’
self.window.run_command(‘undo’)[/code]

0 Likes

#5

[quote=“bizoo”]No, don’t know why it doesn’t works.
[/quote]

My bad, that was because I called run_command() from the active_view() of window so It’s just like a TextCommand.

Thanks for your help, I was able to finish my plugin here: https://github.com/linktohack/VintageYankStack. It’s a Emacs kill ring style for Vintage. I would be happy if you have time give it a look.

0 Likes