Home Download Buy Blog Forum Support

undo, run_command and command_history

undo, run_command and command_history

Postby linktohack on Mon May 27, 2013 8:55 am

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:
Code: Select all
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')


2. 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 http://www.sublimetext.com/forum/viewtopic.php?f=6&t=7917&p=33227&hilit=undo#p33227 which mention
Code: Select all
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.
linktohack
 
Posts: 4
Joined: Mon May 27, 2013 8:23 am

Re: undo, run_command and command_history

Postby bizoo on Mon May 27, 2013 9:13 am

linktohack wrote:2. Is it possible to retrieve the command has been undo? It looks like that command_history just shift it out from the stack

You can use a positive index for the redo stack:
Code: Select all
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.
bizoo
 
Posts: 859
Joined: Wed Dec 08, 2010 6:53 am
Location: Switzerland

Re: undo, run_command and command_history

Postby linktohack on Mon May 27, 2013 10:11 am

bizoo wrote:You can use a positive index for the redo stack:


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

BTW, do you have any idea about the first question? WindowCommand seem allow us to create several undo points as you mentioned here http://www.sublimetext.com/forum/viewtopic.php?f=6&t=10103&p=40088&hilit=undo#p40090, will it has something to do with undo too?
linktohack
 
Posts: 4
Joined: Mon May 27, 2013 8:23 am

Re: undo, run_command and command_history

Postby bizoo on Mon May 27, 2013 10:54 am

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: Select all
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')
bizoo
 
Posts: 859
Joined: Wed Dec 08, 2010 6:53 am
Location: Switzerland

Re: undo, run_command and command_history

Postby linktohack on Mon May 27, 2013 1:40 pm

bizoo wrote:No, don't know why it doesn't works.

My bad, that was because I called
Code: Select all
run_command()
from the
Code: Select all
active_view()
of
Code: Select all
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.
linktohack
 
Posts: 4
Joined: Mon May 27, 2013 8:23 am


Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 3 guests