The purpose of plugin_loaded()
in Sublime Text 3 is to tell plugin code when the API is available, because in ST3 the plugin host is hosted in a separate process and it takes time to get everything spun up and talking to the core. As such there are only a few API calls that you can make prior to plugin_loaded()
being called.
On the flip side, Sublime Text 2 doesn’t have a separate plugin host, and it doesn’t contain this particular endpoint.
Both versions load plugin files right away and execute any top level code in the plugin while the plugin is being loaded, which is why in ST3 you need to guard any API calls from being called too early.
Despite the lack of a plugin_loaded()
module endpoint, I have some anecdotal evidence that calling some API methods (such as sublime.active_window()
too soon in ST2 does indeed return None
and not a valid window. I’ve never done anything in ST2 that needed top level code, so I don’t know if there is an accepted “best practices” for something like this, but a potential solution is this:
import sublime, sublime_plugin
import sys
def plugin_loaded():
# If there is no active window yet, call ourselves again a bit to
# give the API time to settle.
window = sublime.active_window()
if window is None:
return sublime.set_timeout(lambda: plugin_loaded(), 100)
window.run_command("show_panel", {"panel": "console", "toggle": True})
# on ST2, simulate a call to plugin_loaded()
if sys.version_info < (3,):
plugin_loaded()
If loaded in ST3, plugin_loaded()
will be called automatically and the guard code will do nothing, and the console will open at startup. On ST2 an initial call to plugin_loaded()
is made by the top level code (by detecting that the python version is 2) and the code in plugin_loaded()
may trigger the guard code and call itself again until things are ready.
As I mentioned above, I don’t know if this is the expected behaviour here, as I only have the one version of ST2 to test with. Probably this would go a little pear shaped if you’re on a Mac because it’s possible for Sublime to be running with no windows at all. On the other hand ST2 on a Mac is no longer supported, so that may be less of an issue. 