catppuccin / nvim

🍨 Soothing pastel theme for (Neo)vim
MIT License
5.45k stars 237 forks source link

background variable not detected correctly on boot #682

Closed iruzo closed 5 months ago

iruzo commented 6 months ago

Description

I'm using:

When I change my GNOME theme from dark to light, the wezterm theme works fine and change from dark to light. On Neovim, it should change the background variable, which it doesn't change the theme by default, but it does for the next boot of the program (so if I change the theme to light, and restart Neovim, Neovim should be on light), but it doesn't. The background variable is dark.

This works on other plugins like gruvbox.nvim and DoomOne.

In order to test this, the next configuration can be used:

function scheme_for_appearance(appearance) if appearance:find 'Dark' then return 'Catppuccin Macchiato' else return 'Catppuccin Latte' end end

return { color_scheme = scheme_for_appearance(get_appearance()), }

Change your GNOME theme to light, and try to open Neovim, it should be on latte, but it isn't.
In order to see how it works with other themes, I'll leave here a config of DoomOne (lazy):
```lua
  {
    'NTBBloodbath/doom-one.nvim',
    version = '*',
    priority = 1000,
    config = function()
      vim.g.doom_one_cursor_coloring         = true
      vim.g.doom_one_terminal_colors         = true
      vim.g.doom_one_italic_comments         = true
      vim.g.doom_one_enable_treesitter       = true
      vim.g.doom_one_diagnostics_text_color  = true
      vim.g.doom_one_transparent_background  = false

      vim.g.doom_one_plugin_neorg            = true
      vim.g.doom_one_plugin_barbar           = false
      vim.g.doom_one_plugin_telescope        = true
      vim.g.doom_one_plugin_neogit           = true
      vim.g.doom_one_plugin_nvim_tree        = true
      vim.g.doom_one_plugin_dashboard        = false
      vim.g.doom_one_plugin_startify         = false
      vim.g.doom_one_plugin_whichkey         = true
      vim.g.doom_one_plugin_indent_blankline = true
      vim.g.doom_one_plugin_vim_illuminate   = false
      vim.g.doom_one_plugin_lspsaga          = false

      vim.cmd.colorscheme 'doom-one'
    end
  },

Note: I already tried to get the background with vim.opt.background:get() but no luck.

Thanks in advance!

Neovim version

NVIM v0.9.5
Build type: RelWithDebInfo
LuaJIT 2.1.1707061634

Terminal and multiplexer

WezTerm [wezterm 20240203-110809-5046fc22]

Catppuccin version / branch / rev

stable

Steps to reproduce

  1. set system theme to latte
  2. nvim -u repro.lua

Expected behavior

Theme should be on latte when the light mode is set.

Actual behavior

For some reason, the variable won't switch to 'light' and stays in the 'dark' state.

Repro

-- DO NOT change the paths and don't remove the colorscheme
local root = vim.fn.fnamemodify("./.repro", ":p")

-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "cache" }) do
  vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

-- bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({ "git", "clone", "--filter=blob:none", "https://github.com/folke/lazy.nvim.git", lazypath })
end
vim.opt.runtimepath:prepend(lazypath)

-- install plugins
local plugins = {
  {
    'catppuccin/nvim',
    tag = 'stable',
    name = 'catppuccin',
    priority = 1000,
    config = function()
      require'catppuccin'.setup({
        background = { -- :h background
          light = "latte",
          dark = "macchiato",
        },
        term_colors = true,
        integrations = {
          telescope = true,
          mason = true,
          notify = true,
          mini = {
            enabled = true,
            indentscope_color = "",
          },
        },
      })
    end
  },
  {
    'NTBBloodbath/doom-one.nvim',
    version = '*',
    config = function()
      vim.g.doom_one_cursor_coloring         = true
      vim.g.doom_one_terminal_colors         = true
      vim.g.doom_one_italic_comments         = true
      vim.g.doom_one_enable_treesitter       = true
      vim.g.doom_one_diagnostics_text_color  = true
      vim.g.doom_one_transparent_background  = false

      vim.g.doom_one_plugin_neorg            = true
      vim.g.doom_one_plugin_barbar           = false
      vim.g.doom_one_plugin_telescope        = true
      vim.g.doom_one_plugin_neogit           = true
      vim.g.doom_one_plugin_nvim_tree        = true
      vim.g.doom_one_plugin_dashboard        = false
      vim.g.doom_one_plugin_startify         = false
      vim.g.doom_one_plugin_whichkey         = true
      vim.g.doom_one_plugin_indent_blankline = true
      vim.g.doom_one_plugin_vim_illuminate   = false
      vim.g.doom_one_plugin_lspsaga          = false

    end
  },
}
require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

vim.cmd.colorscheme("catppuccin")
-- vim.cmd.colorscheme 'doom-one'
-- add anything else here
rgambord commented 6 months ago

This has been a bug for a long, long time. I was able to fix it in an older commit with this patch:

index f785fdb..27d8282 100644
--- a/lua/catppuccin/lib/highlighter.lua
+++ b/lua/catppuccin/lib/highlighter.lua
@@ -36,7 +36,7 @@ function M.load(theme)
    end
    g.colors_name = "catppuccin"

-   M.properties(theme.properties, "o")
+   -- M.properties(theme.properties, "o")
    M.syntax(vim.tbl_deep_extend("keep", theme.custom_highlights, theme.integrations, theme.syntax, theme.editor))

    if require("catppuccin.config").options["term_colors"] then

Just updated tonight and noticed it's broken still. That module has also been deprecated.

rgambord commented 6 months ago

You can work around the issue by loading the color scheme after the background option is set,

vim.api.nvim_create_autocmd("OptionSet", {
    pattern = "background",
    callback = function()
        vim.cmd("Catppuccin " .. (vim.v.option_new == "light" and "latte" or "mocha"))
    end,
})

Edit: If you are using something like lualine that references catppuccin, you'll also want to put its setup in the callback function, or the colors will be based on mocha (?).

nullchilly commented 6 months ago

I added a quick fix

Try this:

require("catppuccin").setup { flavour = "auto" }

https://github.com/catppuccin/nvim/assets/56817415/3b2f5494-b7cf-4ba1-9d59-d40404cc7f09

iruzo commented 6 months ago

@nullchilly

I gave that a try and something weird happens:

nvim-issue.webm

mrcsmcln commented 6 months ago

Super new to Neovim but I think this is happening for me as well. With flavour = "auto" set, the light theme loads just fine but the dark theme doesn't look right. Running :colorscheme catppuccin-[flavour] produces the ideal result.

I'm using macOS / AstroNvim.

goulf-3m commented 5 months ago
  if (option_was_set("bg") || strequal(p_bg, value)) {
    // background is already set... ignore
    return;
  }
  if (starting) {
    // Wait until after startup, so OptionSet is triggered.
    do_cmdline_cmd((value[0] == 'l')
                   ? "autocmd VimEnter * ++once ++nested :lua if not vim.api.nvim_get_option_info2('bg', {}).was_set then vim.o.bg = 'light' end"
                   : "autocmd VimEnter * ++once ++nested :lua if not vim.api.nvim_get_option_info2('bg', {}).was_set then vim.o.bg = 'dark' end");
  } else {

Referring to the neovim 0.9.5 code, an OptionSet event won't happen if:

  1. bg was explicitly set,
  2. or term bg is dark.

I guess, CAT should respect, but not depend on, OptionSet events, when "auto" is used. Preferably, monitor bg change and respond automatically without restarting nvim.

iruzo commented 5 months ago

@nullchilly

I can confirm that with the recent changes, using require("catppuccin").setup { flavour = "auto" }, it works. I'm gonna close this since it is resolved with that change. In case flavour = "auto" is not already documented on the README, I will do the PR myself.

Tysm guys !