Repository: neovim/neovim
File: runtime/lua/vim/_core/defaults.lua (Lua)

Code before:

            if rr and gg and bb then
              local luminance = (0.299 * rr) + (0.587 * gg) + (0.114 * bb)
              local bg = luminance < 0.5 and 'dark' or 'light'
              setoption('background', bg, force)

              -- On the first query response, don't force setting the option in
              -- case the user has already set it manually. If they have, then
              -- this autocommand will be deleted. If they haven't, then we do
              -- want to force setting the option to override the value set by
              -- this autocommand.
              if not force then
                force = true
              end
            end
          end
        end,
      })

      vim.api.nvim_create_autocmd('VimEnter', {
        group = group,
        nested = true,
        once = true,
        callback = function()
          if vim.api.nvim_get_option_info2('background', {}).was_set then
            vim.api.nvim_del_autocmd(id)
          end
        end,
      })

      vim.api.nvim_ui_send('\027]11;?\007')
    end

    --- If the TUI (term_has_truecolor) was able to determine that the host
    --- terminal supports truecolor, enable 'termguicolors'. Otherwise, query the
    --- terminal (using both XTGETTCAP and SGR + DECRQSS). If the terminal's
    --- response indicates that it does support truecolor enable 'termguicolors',
    --- but only if the user has not already disabled it.
    do
      local colorterm = os.getenv('COLORTERM')
      if tty.rgb or colorterm == 'truecolor' or colorterm == '24bit' then
        -- The TUI was able to determine truecolor support or $COLORTERM explicitly indicates
        -- truecolor support
        setoption('termguicolors', true)
      elseif colorterm == nil or colorterm == '' then
        -- Neither the TUI nor $COLORTERM indicate that truecolor is supported, so query the
        -- terminal
        local caps = {} ---@type table<string, boolean>
        require('vim.termcap').query({ 'Tc', 'RGB', 'setrgbf', 'setrgbb' }, function(cap, found)
          if not found then
            return

Review comment (suggestion):
> Note: To preserve the existing behavior ... this triggers OptionSet manually on VimEnter ... However, I'm unsure if this behavior is truly desired given that the documentation says OptionSet is triggered "After setting an option (except during |startup|)."

Thanks for noting that. It's probably safe to drop this, as a followup PR. *Before* your fix (this PR), it was needed as a workaround since user config may not see the "correct" 'background' during startup. After your fix, that's no longer necessary.

Also, this `nvim_exec_autocmds('OptionSet')` doesn't set `v:option_new` etc so it's not really a full replacement anyway. (We have a backlog item to enhance `nvim_exec_autocmds` so that it can set event-args.)

Code after:
            -- Don't delete the autocmd because the bg response may come
            -- after the DSR response if the terminal handles requests out
            -- of sequence. In that case, the background will simply be set
            -- later in the startup sequence.
            return false
          end

          local r, g, b = parseosc11(resp)
          if r and g and b then
            local rr = parsecolor(r)
            local gg = parsecolor(g)
            local bb = parsecolor(b)

            if rr and gg and bb then
              local luminance = (0.299 * rr) + (0.587 * gg) + (0.114 * bb)
              local bg = luminance < 0.5 and 'dark' or 'light'
              vim.api.nvim_set_option_value('background', bg, {})

              -- Ensure OptionSet still triggers when we set the background during startup
              if vim.v.vim_did_enter == 0 then
                vim.api.nvim_create_autocmd('VimEnter', {
                  group = group,
                  once = true,
                  nested = true,
                  callback = function()
                    vim.api.nvim_exec_autocmds('OptionSet', {
                      pattern = 'background',
                    })
                  end,
                })
              end
            end
          end
        end,
      })

      vim.api.nvim_create_autocmd('VimEnter', {
        group = group,
        nested = true,
        once = true,
        callback = function()
          local optinfo = vim.api.nvim_get_option_info2('background', {})
          local sid_lua = -8
          if
            optinfo.was_set
            and optinfo.last_set_sid ~= sid_lua
            and next(vim.api.nvim_get_autocmds({ id = id })) ~= nil
          then
            vim.api.nvim_del_autocmd(id)
          end
        end,