navarasu / onedark.nvim

One dark and light colorscheme for neovim >= 0.5.0 written in lua based on Atom's One Dark and Light theme. Additionally, it comes with 5 color variant styles
MIT License
1.61k stars 163 forks source link

Override Colors on Per Theme Basis #70

Open mike-lloyd03 opened 2 years ago

mike-lloyd03 commented 2 years ago

Thanks for this plug. It works really well. Is there a way to override colors on a per-theme basis?

Example: I'd like to override bg1 but only on the dark theme and leave the light theme as default.

require("onedark").setup({
    style = "dark",
    toggle_style_list = { "dark", "light" },
    colors = {
        bg0 = (vim.g.onedark_config.style == "dark") and "#1a1c23" or "#fafafa",
    },
}

This obviously doesn't work because setup configs are only evaluated upon initialization. But some way to do this would be nice.

Thanks!

dev-msp commented 2 years ago

This is possible without an update to the plugin (though it should probably support this use case anyway). You can take advantage of the ColorSchemePre autocmd event to update your plugin config before it's used to apply the theme.

config = function()
  local onedark = require("onedark")
  local default_style = "dark"

  local function get_theme_overrides(style)
    local palette = require("onedark.palette")[style]
    if type(palette) == "nil" then
      vim.notify(string.format("couldn't get palette for style %q", style), vim.log.levels.ERROR)
      return
    end
    return {
      highlights = {
        -- ....
      },
    }
  end

  local augroup_id = vim.api.nvim_create_augroup("OnedarkStyle", {})

  vim.api.nvim_clear_autocmds({ group = augroup_id })
  vim.api.nvim_create_autocmd("ColorSchemePre", {
    group = augroup_id,
    desc = "Apply theme overrides before colorscheme change",
    callback = function()
      if vim.g.onedark_config and vim.g.onedark_config.loaded then
        local overrides = get_theme_overrides(vim.g.onedark_config.style)
        onedark.set_options("highlights", overrides.highlights)
        -- onedark.set_options("colors", overrides.colors)
        -- etc
      end
    end,
  })

  onedark.setup({
    toggle_style_list = { "dark", "light" },
    highlights = get_theme_overrides(default_style).highlights,
  })
  onedark.load()
end
zhengpd commented 1 year ago

Also like this. It would be easier to set up if onedark has builtin support for per-theme overrides.