Sublime Forum

Alternative for per-host settings

#1

I found a couple of topics related to this but none of the proposed solutions fit everything I need: I sync settings across machines but have different settings for different plugins (mostly just executable paths). Problem is most plugins retrieve their settings by sublime.load_settings() and just once at startup. So not being familiar with the API I came up with this ‘hack’ (replace sublime.load_settings with my own function), put in a 1_PerHostSettings.sublime-package file to get it loaded a.s.a.p.:

import json
import os
import socket
import sublime

def LoadPerHostSettings(settingsFile):
  origSettings = sublime.settings_loader(settingsFile)
  baseName = os.path.splitext(settingsFile)[0]
  hostname = socket.gethostname().lower()
  hostSettingsFile = os.path.join(sublime.packages_path(), "User", "{}.{}.json".format(baseName, hostname))
  try:
    with open(hostSettingsFile) as f:
      hostSettings = json.load(f)
    for key, value in hostSettings.items():
      print('PerHostSettings', 'replacing', origSettings.get(key), 'with', value)
      origSettings.set(key, value)
  except Exception:
    pass
  return origSettings

if not hasattr(sublime, 'load_settings_overloaded'):
  sublime.load_settings_overloaded = True
  sublime.settings_loader = sublime.load_settings
print('PerHostSettings', 'swapping', sublime.load_settings, 'with', LoadPerHostSettings)
sublime.load_settings = LoadPerHostSettings

This works, but I’m not that keen with modifying the sublime module itself. Are there other and better ways of doing this?

1 Like

#2

Problem is most plugins retrieve their settings by sublime.load_settings() and just once at startup.

Settings objects are live and should update automatically. Is this not happening under certain conditions?

0 Likes

#3

Another method that comes to mind is using a plugin with a plugin_loaded() endpoint (but still in a similarly named package so it loads before other installed packages) to copy the related settings file over the default one in the User package, so that whenever the package loads (e.g. when Sublime starts) it would set up “native” settings for your host.

You could also make an explicit command that you have to run that does the copy for you and then manually take the action whenever you switch hosts, which may or may not be objectionable depending on how often you do that. It also has the down side that you could forget to do it and run in an inappropriate state.

I suspect the problem in this case is less this and more that some plugins tend to cache the loaded value of the setting instead of the settings object (perhaps not realizing that only one is ever created anyway).

That said there did used to be issues related to symlinked resources not being reloaded when they change, though I think that is fixed now (I don’t recall what build, though).

1 Like

#4

Good point about the live settings updating, if there’s a hook for that it might be usable instead?

0 Likes

#5

Thing is I don’t want to copy settings files: I want to merge them. This allows having one setting file with all common settings and then per-host files with only the settings which are different…

1 Like