Iron-E / nvim-highlite

A colorscheme generator that is "lite" on logic for the developer.
Other
240 stars 33 forks source link

Question changing highlights #9

Closed Th3Whit3Wolf closed 3 years ago

Th3Whit3Wolf commented 3 years ago

Sorry to bug you, I have an issue where the user can not change the highlight in one-nvim but can in onebuddy.

I was able to change highlights by either

or

The user was unable to change the highlighting using either of these methods. Do you know if there are anything that colorbuddy does that would allow this where nvim-highlite may not? I am pretty puzzled why this won't work.

Iron-E commented 3 years ago

I'll take a look and see what the issue might be. Currently I know that after Neovim loads I can run hi! and either link or override what was set by nvim-highlite. I'll try embedding the command into a minimal RC so I can replicate the issue.

Iron-E commented 3 years ago

I can get the bug to happen with this minimal config:

packadd nvim-highlite
set termguicolors "optional
colorscheme highlite

hi! Error guifg=#000000 guibg=#FFFFFF

I'll keep investigating.

Edit

Adding this to the top of the file:

augroup Highlite
    autocmd!
    autocmd ColorScheme * call getchar()
    autocmd ColorScheme * echom "loaded '".expand("<amatch>")."' from '".expand("<afile>")."'"
augroup end

…reveals that the colorscheme is loaded twice. That's probably why the bug is occuring— maybe.

Edit 2

The above examples don't work with default colorschemes either:

augroup Highlite
    autocmd!
    autocmd ColorScheme * call getchar()
    autocmd ColorScheme * echom "loaded '".expand("<amatch>")."' from '".expand("<afile>")."'"
augroup end

set termguicolors
colorscheme slate

hi! Error guifg=#000000 guibg=#FFFFFF

Seems like it could be something with Neovim in general that Colorbuddy has a workaround for, since I can confirm the following works:

augroup Highlite
    autocmd!
    autocmd ColorScheme * call getchar()
    autocmd ColorScheme * echom "loaded '".expand("<amatch>")."' from '".expand("<afile>")."'"
    autocmd ColorScheme * hi! Error guifg=#000000 guibg=#FFFFFF
augroup end

set termguicolors
colorscheme slate

Perhaps there should be a similar workaround for nvim-highlite? Something like highlite.highlight_always() which performs highlite.highlight() with the arguments on ColorScheme events. It would look like this:

packadd nvim-highlite
set termguicolors "optional
colorscheme highlite

lua << EOF
    local highlight = require('highlite').highlight_always

    highlight('Error', {fg="#000000", bg="#FFFFFF"})
    -- other highlights below
EOF
Th3Whit3Wolf commented 3 years ago

I think mentioning how to do it on the README or in a wiki would be fine. It puts whatever the cost of the function is onto the minority of the people that want it.

Iron-E commented 3 years ago

Does the referencing commit add instructions which seem clear enough?

Th3Whit3Wolf commented 3 years ago

Yeah I think so

Th3Whit3Wolf commented 3 years ago

In the future I see lua being more commonplace in neovim configs and with people using init.luas more I am curious how you would write this in lua. I found two ways.

The ugly way

vim.api.nvim_command("augroup Highlight")
vim.api.nvim_command("autocmd!")
vim.api.nvim_command("autocmd ColorScheme highlite hi! Normal guifg=#000000 guibg=#FFFFFF")
vim.api.nvim_command("augroup end")
vim.api.nvim_command("colorscheme highlite")

The prettier way

local vim, api, cmd = vim, vim.api, vim.cmd

local function augroups(definitions)
    for group_name, definition in pairs(definitions) do
        api.nvim_command("augroup " .. group_name)
        api.nvim_command("autocmd!")
        for _, def in ipairs(definition) do
            local command = table.concat(vim.tbl_flatten {"autocmd", def}, " ")
            api.nvim_command(command)
        end
        api.nvim_command("augroup END")
    end
end

local autocmds = {
    Highlight = {
        {"ColorScheme", "highlite", "hi! Normal guifg=#000000 guibg=#FFFFFF"}
    }
}

augroups(autocmds)
cmd 'colorscheme highlite'

The second way also blends in more with the config if you use the augroups function for generating all of your autocmds.

I'm not very experienced with lua thought so I'm curious if you have a nice way of doing it.

Iron-E commented 3 years ago

I definitely see Lua being the way to go in the future too. If you use multiline strings and nvim_exec it's a little prettier:

vim.api.nvim_exec[[
    augroup Highlight
        autocmd!
        autocmd ColorScheme highlite hi! Normal guifg=#000000 guibg=#FFFFFF
    augroup end
    "more vimscript
]]

I believe the Neovim team is working on a wrapper for autocommands in Lua. After that is complete you'll be able to do this even more easily (although we don't know what it looks like yet).