How to get user input before saving a file in Sublime Text

Posted by EddieJessup on Stack Overflow See other posts from Stack Overflow or by EddieJessup
Published on 2014-06-12T23:00:05Z Indexed on 2014/06/13 15:25 UTC
Read the original article Hit count: 328

I'm making a plugin in Sublime Text that prompts the user for a password to encrypt a file before it's saved. There's a hook in the API that's executed before a save is executed, so my naïve implementation is:

class TranscryptEventListener(sublime_plugin.EventListener):
    def on_pre_save(self, view):
        # If document is set to encode on save
        if view.settings().get('ON_SAVE'):
            self.view = view
            # Prompt user for password
            message = "Create a Password:"
            view.window().show_input_panel(message, "", self.on_done, None, None)

    def on_done(self, password):
        self.view.run_command("encode", {password": password})

The problem with this is, by the time the input panel appears for the user to enter their password, the document has already been saved (despite the trigger being 'on_pre_save'). Then once the user hits enter, the document is encrypted fine, but the situation is that there's a saved plaintext file, and a modified buffer filled with the encrypted text.

So I need to make Sublime Text wait until the user's input the password before carrying out the save. Is there a way to do this?

At the moment I'm just manually re-saving once the encryption has been done:

def on_pre_save(self, view, encode=False):
    if view.settings().get('ON_SAVE') and not view.settings().get('ENCODED'):
        self.view = view
        message = "Create a Password:"
        view.window().show_input_panel(message, "", self.on_done, None, None)

def on_done(self, password):
    self.view.run_command("encode", {password": password})
    self.view.settings().set('ENCODED', True)
    self.view.run_command('save')
    self.view.settings().set('ENCODED', False)

but this is messy and if the user cancels the encryption then the plaintext file gets saved, which isn't ideal. Any thoughts?

Edit: I think I could do it cleanly by overriding the default save command. I hoped to do this by using the on_text_command or on_window_command triggers, but it seems that the save command doesn't trigger either of these (maybe it's an application command? But there's no on_application_command). Is there just no way to override the save function?

© Stack Overflow or respective owner

Related posts about encryption

Related posts about sublimetext