Skip to content

Cannot take advantage of neovim's auto-detection of termguicolors #210

@lo48576

Description

@lo48576

Issue

Even without any config, termguicolors is automatically enabled by detecting terminal cpabilities since neovim v0.10.
However, it seems to be enabled after the UI is initialized (e.g., after UIEnter or something like that) and might not be set yet at the time BufReadPre autocmds are executed for the first buffer.
So, with this plugin being lazy-loaded, auto-detection of termguicolor and auto-enabled colorization of the first buffer conflicts.

How to reproduce

Pattern 1: for BufReadPre (with neovim 0.12)

-- init.lua
vim.api.nvim_create_autocmd('BufReadPre', { once = true, callback = function()
  vim.pack.add({ 'https://github.com/catgoose/nvim-colorizer.lua' })
  require('colorizer').setup({
    user_commands = true,
  })
end })

Result: Colorizer: Error: &termguicolors must be set

Pattern 2: for UIEnter (with neovim 0.12):

-- init.lua
vim.api.nvim_create_autocmd('UIEnter', { once = true, callback = function()
  vim.pack.add({ 'https://github.com/catgoose/nvim-colorizer.lua' })
  require('colorizer').setup({
    user_commands = true,
  })
end })

Result: the first buffer loaded before the UI init is not colorized.

$ echo 'body { background: red; }' >test.css
$ nvim test.css
Image

Note that starting neovim by just nvim and opening the file later by :e test.css makes the file colorized, since the buffer is created after the UI and the plugin are initialized.

Request

I'm not sure how this should be solved, but some candidates are:

  • Not aborting when termguicolors is false, wait for the UI to be initialized and termguicolors is finalized, and then initialize the part that require termguicolors to be true.
    • Current: BufReadPre with termguicolors=false => colorizer plugin loaded => (failed!)
    • New: BufReadPre with termguicolors=false => colorizer plugin loaded => neovim automatically sets termguicolors=true => UIEnter => colorizer initialization and auto-attach resumes!
  • Provide a function to "attach to the buffer if the plugin should have attached automatically".
    • Note that require('colorizer').is_buffer_attached(0)) is false at the timing of UIEnter (see below).
    • require('colorizer').attach_to_buffer() may attach to the buffer anyway, even if the buffer is not in filetypes or even if excluded.
    • With this function, user can retry auto-attach manually at after require('colorizer').setup().
      • Doing this automatically in require('colorizer').setup() is also good?
-- init.lua
vim.api.nvim_create_autocmd('UIEnter', { once = true, callback = function()
  vim.pack.add({ 'https://github.com/catgoose/nvim-colorizer.lua' })
  require('colorizer').setup({
    user_commands = true,
  })
  vim.print(require('colorizer').is_buffer_attached(0))
end })

With this config, nvim test.css may print false, and require('colorizer').reload_all_buffers() don't enable the highlight for test.css.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions