Sublime Forum

Toggle Vintage package

#1

I want to find the best way to toggle the Vintage package. That is, to turn it on and off using a keyboard shortcut.

My first pick was to use the plugin by Andrew Siegman:

# https://gist.github.com/blunket/e041565ef50b925ea68f29eb495e4433

import sublime
import sublime_plugin

class ToggleVintageCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        settings = sublime.load_settings('Preferences.sublime-settings')
        ignored = settings.get("ignored_packages")
        if "Vintage" in ignored:
            ignored.remove("Vintage")
        else:
            ignored.append("Vintage")
        settings.set("ignored_packages", ignored)

But then I realized that some settings can be toggled without using any plugin. For example:

{ "keys": ["super+shift+w"], "command": "toggle_setting", "args": { "setting": "word_wrap" } },

I understand, of course, that this approach won’t allow me to remove "Vintage" from "ignored_packages":

// This won't happen
FROM: "ignored_packages": ["Foo", "Vintage", "Bar"]
TO:   "ignored_packages": ["Foo", "Bar"]

But maybe it will allow me, at least, to add/remove the

"ignored_packages": [],

line? Since I don’t use any other packages, this little trick will be enough for me.

0 Likes

#2

The toggle_setting command can only toggle the value of boolean settings on and off. Additionally it adjusts view settings (that is, settings specific to the current tab, such as flipping the state of word wrap, which happens only for the current file).

As such, it won’t allow you to toggle this setting because it’s not a boolean, and since ignored_packages is an Application Behaviour setting, it also does not apply here.

The best bet would be a plugin similar to the one that you outlined above, since that actually adjusts the global preferences. Note however that it doesn’t save the preferences setting, it only adjusts the settings as they were loaded (if that matters).

It doesn’t apply to your specific problem here (since it also deals exclusively with view settings), but the following plugin is a version of the toggle_setting command that can toggle any view setting between any arbitrary values, which is at least tangentially related and could be useful in general for other purposes.

1 Like

#3

This is great, thanks a lot. But could you explain why the following doesn’t work for me?

{
  "keys": ["ctrl+alt+v"],
  "command": "toggle_setting_ext",
  "args":
  {
    "setting": "ignored_packages",
    "options": [["Vintage"], []]
  }
},
0 Likes

#4

That’s because as mentioned above both that plugin and the internal toggle_setting command only adjust settings that are specific to open files, like rulers, word wrap, and such.

Whether or not a package is loaded into the plugin system or not is not a file by file choice but instead global to the application as a whole. So the setting has to be altered in Preferences.sublime-settings (which is what that first plugin you posted is doing).

So, I would just go with that one; I only included the second one because it has the power to toggle other settings, like the actual ruler settings (instead of just turning them off and on) and other settings that are specific to a file but are not just on and off flags.

Sorry for the confusion there

1 Like

#5

Thanks again. And the last one question:

Could you explain why the original plugin doesn’t work properly if I simply start Sublime (without any tabs) and press Ctrl-Alt-V (Control-Option-V on a Mac) twice? ST enables Vintage, but for some reason it refuses to disable it.

Seems to be a bug. How it can be fixed to make this plugin work without this little pitfall?

0 Likes

#6

Potential reasons for that could be:

  1. The key binding does something different when Vintage is enabled; you could check this by opening the Sublime console with View > Show Console , enter sublime.log_commands() and then press the key to see if the command name is logged as you expect.

  2. If the plugin is not loaded yet, the key binding won’t do anything. This seems unlikely though, if it toggles the state and then does not turn it off.

  3. Possibly it gets out of sync because the plugin alters the setting, but does not persist it to disk, so there could be some weirdness at startup with things not being entirely in the state that is expected. You could potentially solve that by adding sublime.save_settings("Preferences.sublime-settings") as the last line in the method.

That said, I tested here (using Linux) and it seemed to work fine regardless of what I did (and without modifying the plugin code from what was presented above).

0 Likes

#8

To make sure we really understand each other correctly, here is an animated gif. I wasn’t able to add it right here, so I uploaded it on Imgur: https://imgur.com/a/CTJHuv2. If you don’t like Imgur (lots of ads, etc.), here is a copy on my GitHub: https://github.com/johnn2025/test/blob/main/sublime.gif

If using sublime.log_commands(), the log is:

// Here I press Ctrl-Shift-V for the 1st time to enable Vintage
ignored packages updated to: []
reloading plugin Vintage.vintage
reloading plugin Vintage.vintage_commands
reloading plugin Vintage.vintage_motions

// Here I press Ctrl-Shift-V for the 2nd time and cannot turn it off
ignored packages updated to: ["Vintage"]
unloading plugin Vintage.vintage
unloading plugin Vintage.vintage_commands
unloading plugin Vintage.vintage_motions

First, I didn’t have "ignored_packages" line in my preferences. ST started with Vintage turned off.

Then I added "ignored_packages": [] in my preferences and repeated the test. ST started with Vintage, of course. And then, after I pressed Ctrl-Shift-V to turn Vintage off, it didn’t work either. Log:

// Here I press Ctrl-Shift-V for the 1st time to disable Vintage. Doesn't work
ignored packages updated to: ["Vintage"]
reloading settings Packages/Vintage/Preferences (OSX).sublime-settings
reloading settings Packages/Vintage/Preferences.sublime-settings
unloading plugin Vintage.vintage
unloading plugin Vintage.vintage_commands
unloading plugin Vintage.vintage_motions

// Here I try again. Doesn't work
ignored packages updated to: []
reloading settings Packages/Vintage/Preferences (OSX).sublime-settings
reloading settings Packages/Vintage/Preferences.sublime-settings
reloading plugin Vintage.vintage
reloading plugin Vintage.vintage_commands
reloading plugin Vintage.vintage_motions

The tests were performed on a fresh ST profile.

0 Likes

#9

(post withdrawn by author, will be automatically deleted in 24 hours unless flagged)

0 Likes

#10

If I add the sublime.save_settings("Preferences.sublime-settings") line, as you suggested, then after the first Control-Option-V, the part

"ignored_packages": ["Vintage"]

changes to

"ignored_packages": []

And then, after the second Control-Option-V, it changes back to

"ignored_packages": ["Vintage"]

But in fact Vintage is still enabled. And it is enabled in a weird way: at this moment of time, you won’t be able to switch to another mode, e.g. from Insert to Normal or from Normal to Insert

0 Likes