Sublime Forum

How to hook the new show settings event?

#1

How to hook the new show settings event?

It was requested on the core, https://github.com/SublimeTextIssues/Core/issues/1392, but was redirected to a package creation.

I want to add option to disable the minimap and/or lines numbers (if actived) when opening the new Settings. This is useful to save space, as it is a side by side view, and line numbers as minimap, are not really necessary for a good settings editing.

But how I can hook when the new settings file view is show, to trigger the command to hide things temporarily?

0 Likes

#2

The commands that are used for this new functionality are edit_settings and edit_syntax_settings, both of which are stored (along with support code) in Packages/Default/settings.py. Note however that (at least currently) edit_syntax_settings invokes edit_settings to do it’s job, so you really only need to worry about the one command.

A plugin like the following can be used to detect when this command is either just about to happen or has just finished happening:

import sublime
import sublime_plugin

class SampleListener(sublime_plugin.EventListener):
    def on_window_command(self, window, command, args):
        if command == "edit_settings":
            print ("About to execute " + command)

    def on_post_window_command(self, window, command, args):
        if command == "edit_settings":
            print ("Finished executing " + command)

The documentation for these events only mentions that they trigger on the execution of a WindowCommand and the edit_settings is defined as an ApplicationCommand but this still works. I don’t know if that is an omission in the documentation or a bug in sublime or what, though.

3 Likes

#3

Thanks for the time and sample coding!

1 Like

#4

Also note that the python code stores a specific setting value in one (or both?) of the settings views, which may allow you to uniquely identify the opened window, together with the command’s argument.

2 Likes

#5

I implemented it like this:

import sublime
import sublime_plugin


class SampleListener(sublime_plugin.EventListener):
    """
    It requires the packaga `DistractionFreeWindow` installed and configured to hide the SideBar and
    the Mini Map.
    
    The dirt trick here is to put the current window on the `distraction_free_window` mode on the
    `on_window_command` pre command hook, then the new setting windows will inherited it.
    
    Later on the `on_post_window_command` post command hook, we set the current window back from
    the `distraction_free_window` mode.
    
    How to hook the new show settings event?
    https://forum.sublimetext.com/t/how-to-hook-the-new-show-settings-event/23793/2
    http://docs.sublimetext.info/en/latest/extensibility/commands.html

    Add option to disable the minimap and/or lines numbers (if actived) when opening the new Settings 
    https://github.com/evandrocoan/SublimeTextStudio/issues/12
    """
    
    def on_window_command(self, window, command, args):
        
        # print ("About to execute " + command)
        
        if command == "edit_settings":
            
            # print ("EXECUTING...")
            window.run_command("distraction_free_window")


    def on_post_window_command(self, window, command, args):
        
        # print ("Finished executing " + command)
        
        if command == "edit_settings":
            
            # print ("EXECUTING...")
            window.run_command("distraction_free_window")
0 Likes