Sublime Forum

Disable Saving of File

#1

Am trying to disable save command though a plugin. Tried below but did not work.

class PHPLinter(sublime_plugin.EventListener):
    def on_post_save(self, view):
        return False
0 Likes

#2

“post save” means “after save”, so you can’t disable something that has already happened.
You probably want on_pre_save instead. Doesn’t seem like it accepts a boolean return value to prevent saving though…

1 Like

#3

As far as I am aware, on_(post|pre)_save hooks are fired after/before the save no matter what you do. I don’t think you can disable those.


What can be done though is using a ViewEventListener to restrict only those views where you want to disable save and then intercepting the save command and executing noop in on_text_command hook.

Something like

import sublime
import sublime_plugin


class DisableSaveListener(sublime_plugin.ViewEventListener):

    @classmethod
    def is_applicable(cls, settings):
        syntax = settings.get("syntax")
        return syntax == "Packages/Text/Plain text.tmLanguage"

    def on_text_command(self, command_name, args):
        if command_name == "save":
            return ("noop", {})
        return None

Ideally, this should disable save on plain text files (see is_applicable), but unfortunately, I can’t get this to work on Windows on the latest dev build.

2 Likes

#5

Called when a text command is issued. The listener may return a (command, arguments) tuple to rewrite the command, or None to run the command unmodified.

So may return ('noop', {}).

0 Likes

#6

Instead of def is_applicable(cls, settings): shuold be def is_applicable(self, settings): but still not working on windows 10 ST4.

0 Likes

#7

Command name save is not detected. All other commands are detected:
drag_select
left_delete

Also needed to comment out is_applicable and @classmethod. i already have a file with .python-version 3.8 and tried changed version to 3.3 but still not working

0 Likes

#8

Ah, thanks ! That explains why. However, even with returning ("noop", {}), it still doesn’t function. Not sure why.

0 Likes

#9

is_applicable is a class method, so it would receive the class as it’s first argument and not an instance of the class i.e. self

0 Likes

#10

Yes, because I made a silly mistake on line 14. It was returning None, which basically means to run the save command as is. So the plugin was useless. As pointed out, it should return ("noop", {}) to basically nullify save (noop is a non existent command)

0 Likes

#11

Still not detecting the save command. Therefore not blocking.

0 Likes

#12

Yes. As I said, no idea why it isn’t working. It could either be a bug that save is not being caught in on_text_command or I have done some mistake in the plugin that I am not able to catch so far.

0 Likes

#13

is save a text command or window command?

0 Likes

#14

Neither on_text_command nor on_window_command respond to save, so I am not sure :man_shrugging:

0 Likes