Auto-load themes for neovim#346
Conversation
|
Awesome! Been looking for this. I'm wondering if there isn't a way for us to upstream the function to do live theme changes to LazyVim directly? I'd rather not maintain a fork if we can get away with not doing that. Alternatively, we could put that function into our own LazyVim checkout, but still basing it on their package. Might that be possible? |
|
I'll investigate. |
|
@dhh We are really just changing two files in the starter: what if we have these two modified files in a lazyvim folder in our repo: config/lazyvim then copy them as part of the nvim.sh script ( install/development/nvim.sh ) to replace the default files cloned from the official starter. ` cp ~/.local/share/omarchy/config/lazyvim/lazy.lua ~/.config/nvim/lua/config/ Sound better? |
a4ce003 to
c0fe94e
Compare
|
I changed the starter repo back to the original, and implemented the copy method mentioned above. |
|
Sorry - I'm not super familiar with omarchy's internals, but I had a few questions:
|
| local signal = uv.new_signal() | ||
| uv.signal_start(signal, "sigusr1", function() | ||
| vim.schedule(function() | ||
| vim.cmd('doautocmd User OmarchyThemeReload') |
There was a problem hiding this comment.
you can just call the reload function here directly
| local last_theme_target = nil | ||
|
|
||
| -- Function to reload the theme based on Omarchy's current theme | ||
| function ReloadTheme() |
There was a problem hiding this comment.
would recommedn putting this in an omarchy folder/path (in nvim) so that you can do require("omarchy").reload_theme() or possibly creating an Omarchy global at the least so that you can namespace this function. ReloadTheme is a bit generic IMO
c0fe94e to
60a1c0e
Compare
|
Based on comments from @tjdevries I refactored the code to separate the function and the themes into an Omarchy folder in nvim/lua. So we're not touching LazyVim files anymore. Thanks TJ for the review. @dhh this separation of code to omarchy's own folder also helps us figure out a way to add other themes to the lua/omarchy/init.lua file when the user installs third party themes. Let me know what you think. |
|
How does this work if the user installs an extra theme? Then it does just what we're doing now the first time? But after that it'll work? |
The nvim theme & colorscheme name must be added to the list in the lua/omarchy/init.lua file (we can automate this in the install theme script). Once it's there, Lazy will install it on first launch and it'll work after that. The only issue is that if the user is using a theme and then modifying/ovverriding the values, it's not gonna use those modifications, it'll just use the base colorscheme. |
|
Hmm, wish we could make it work like before where it could delegate to the theme nvim, if one is present. And if not present, we can assume that the theme is present in the preconfigured list. I like really way to sed an existing file with extra theme installs in and out if we can avoid it. |
|
Something I played with a couple of days ago was that you can trigger changing themes by just There are some issues that I haven't worked out in here but in my head, there's some combination we should be able to achieve of like a symlinked The install of the theme can be handled completely from the plugin file as the Again...not fully baked by any stretch and I'm sure something @tahayvr has explored but wanted to get some thoughts and findings out of my head that I think could be helpful here before I forgot them. |
bc32875 to
8bbff3a
Compare
|
Ok, I take it back! Seems like it is actually using the overrides in the neovim.lua file in themes. You can look at the ristretto theme mini-icons for testing. The only consideration is that the themes must specify a colorscheme like so: {
"LazyVim/LazyVim",
opts = {
colorscheme = "bamboo",
},
},I've added this to the 2 new themes and Catppuccin. We can put a comment in the neovim.lua files that notes this for anyone trying to make themes. @dhh and @ryanrhughes can you test this solution and let me know if it works on your end? It's been a bumpy journey but "Per aspera ad astra". |
|
Thanks @dhh for testing. This can be fixed by adding a As for new themes, I need to check how the Once it's in the list in This basically is like a user has installed multiple themes and can switch between them from within neovim because they are already installed, loaded, and available. We are just automating the install/load/switching. |
|
Awesome! Thanks for pursuing this all the way. It is annoying that you have to restart neovim right now to pickup the new theme. This is going to be much nicer 👌 |
|
I spent some time with this and tried a bit of a different take building on top of this concept to give us some options to look at. The one thing I really don't like about this current setup is the requirement to maintain sort of a registry of themes in Neovim so I wanted to see what it'd take to eliminate that entirely. I've managed to find a way to remove all of that and just add logic to:
In the event the theme is installed and just not called in a plugin, it still sticks around unless you run a dev...ryanrhughes:omarchy:reload-neovim-themes Sticking with the registry may be the simpler path in the end given the constraints we have in the current Lazy setup but figured it was worth a good swing for the learnings. There is also some way we could possibly make that registry just a bunch of symlinks by having something like Regardless, we'll probably want to pull this bit over as just setting the colorscheme, as @dhh noticed doesn't fully apply our theme since we have an after plugin that runs. We need to re-run that after the theme is updated. |
61341fa to
1378d57
Compare
|
Added a Also added a new @ryanrhughes I'm going to look into your suggestions (much appreciated), but curious if you are still having style issues with the new updates to this PR. |
ryanrhughes
left a comment
There was a problem hiding this comment.
@tahayvr I added some more pointed feedback after some deeper testing / review. Let me know if anything doesn't make sense.
We're also running into a situation I also faced when playing with this concept where if open Nvim windows, the plugin doesn't get installed or loaded properly. I built a working concept that had one instance started the theme install drop a lock file so the others would know to wait until that was done to attempt to load the newly installed plugin set the colorscheme. We can look at implementing something similar or just accept that this is a situation that will exist and you still have to restart Nvim after installing a new theme.
I also spent a little bit of time with my idea of splitting the specs and using symlinks. I don't love it.
It requires that we split the plugin definition from the colorscheme definition which means a neovim-plugin.lua and neovim-colorscheme.lua in each theme. We can't just link all of the existing theme files, then have one set as current-theme.lua because if you load multiple colorscheme settings in plugins at once, it's hell and you wind up with highlight groups from other themes sometimes showing up.
The advantage of this was that the extent of omarchy-theme-neovim-add would just be as simple as symlinking the file which would be quite a bit simpler at the expense of having 2 files, and requiring all themes to date to be updated.
At least doing it the way you are here, we're just shoving the plugin info into a single .lua plugin file and it's backwards compatible vs requiring all themes to be updated to a new spec that isn't really tangibly any better.
f9f6564 to
4f572a4
Compare
|
I added a function that checks for the light.mode file in theme folders and sets @dhh I think we're ready! The only thing to add in the Manual is: Themes have to specify the colorscheme in their {
"LazyVim/LazyVim",
opts = {
colorscheme = "bamboo",
},
} |
4f572a4 to
b99e4ee
Compare
|
@tahayvr this is super close in my opinion. I left a few comments in the code and a few below that I observed. Loving how this is shaping up. SO much nicer to not have to restart Neovim all the time! Small Artifact RemainingThere's one very small artifact that isn't getting changed I noticed. It's really only noticeable when going from light to dark so like Latte to Matte Black or vice versa. Would be nice to find how to clobber this one too. Install.colorschemeIt looks like we still need to remove all of the I suppose we can keep it if we'd like but it just seems like something that can go wrong that offers no benefit. This does not impact whether or not the plugin and colorscheme itself is actually loaded and available -- that is handled by the plugin definition. Ref: https://lazy.folke.io/configuration Error on LoadIf we load Nvim with a theme that has been installed before this update, we get this error and the theme doesn't load at all. This is a bit odd since it meets the spec but appears that there's something causing it to not load. Reinstalling the theme causes it to work fine. Ref: https://github.com/perfektnacht/omarchy-wasteland-theme
Error when SwitchingLikely related to the above but if I switched to a theme that was installed before, this error comes up but the theme does seem to apply correctly. We may just need to do some error handling or something. Ref: https://github.com/bjarneo/omarchy-ash-theme
Parsing ErrorIn installed the new Azure Glow theme and encountered a few errors. I believe they're driven in part by how that theme is implementing Neovim colorsheme but maybe something we should try to catch if we can or consider how we fallback gracefully in this situation. Ref: https://github.com/Hydradevx/omarchy-azure-glow-theme/blob/main/neovim.lua It added this to my
Migrating Existing Installed ThemesWith the errors above, it raises the question; can we / should we handle some of this via migration? It seems plausible that a lot of people will have installed themes and feels bad to add a feature that doesn't work unless you reinstall all of your themes. Could also be totally fair to say that we only support this out of the box for core themes but if it's easy enough to add a migration to do a onetime parsing of everything that isn't a symlink, I'd be for that. |
844badf to
9234d20
Compare
|
Fixed the conflict caused by the new file structure. |
|
Why we need this ? |
|
There's no meaningful bloat by having some themes installed but not loaded. Totally worth it for fast theme switching. |
|
Would it be possible to make a neovim plugin out of this? I prefer to have my own neovim config and I think other people would like this too. |
|
I really appreciate how far you've pushed on this, Taha, but this is an enormous amount of code. This shouldn't be this hard. The reason it is is that we're trying to make all these code-dumps that just happen to change colors work. I think we need to go back to the drawing board. Come up with a unified format for changing nvim colors, then build a single plugin that can take this specification, live reload changes, and take the single color definition as an input. |
|
I get it. Most of the code is the scripts around it which grew cause we want to support community themes. The lua files aren't that big themselves and I can totally make them a third-party plugin. Issue is we still need to tell LazyVim which colorschemes we need to have loaded (so it doesn't load & clean them all the time). I can try to unify all the features into a single plugin. At the moment, I'm not sure if it's possible. Worth a try tough. Should I investigate this? |
|
I think what we need to do here is actually to come up with an entirely new nvim plugin that's a generic "will load a given color file", and then we just don't use any of all the existing plugins we've been using before. Then maybe we could actually even just make a single mega plugin that has as many theme files as we can find AND the power to load a custom one. |
|
Let's close this out for now, though. I'm convinced we should fix this actually with a new nvim plugin that does all this work. Then we can include that in Omarchy. I'm not giving up until we get to the promised land here!!! 😄 |







This PR adds an automatic reload feature for NeoVim themes when you change your Omarchy theme.
omarchy-theme-reload.mp4
Omarchy lazyvim starter repo
(feel free to fork this repo into your own github and switch the git clone command in install/development/nvim.sh)
How does the automatic reload feature work?:
In order to enable the automatic reload feature, each theme in Omarchy must have a neovim.lua file that defines the themes like this:
Additionally, we must make sure that LazyVim is configured to load every theme on startup. This is done by setting the
specandinstalloptions in the LazyVim configuration.Then, we need a function to reload the theme based on Omarchy's current theme symlink target. This function will check if the theme has changed and apply the new theme without restarting Neovim.
How does Neovim know when to reload the theme?:
We can use a signal handler that listens for the
SIGUSR1signal. When this signal is received, it will trigger theReloadThemefunction.and finally, we need to send the
SIGUSR1signal to Neovim when the theme is changed. This can be done using a shell command or a script that changes the theme symlink and sends the signal to Neovim.