nvim: Dynamically source colorscheme from file

Neovim will source the `colorscheme.lua` file in its state directory on
startup, as well as whenever the file contents are changed.
This allows any colorscheme definition to be put into the file and vim
will apply it as soon as the file contents change.
This commit is contained in:
Marty Oehme 2023-03-03 18:35:21 +01:00
parent c13db186cc
commit 25d37d17b3
Signed by: Marty
GPG key ID: EDBF2ED917B2EF6A

View file

@ -7,10 +7,28 @@ vim.api.nvim_set_var('gruvbox_italic', 1)
vim.api.nvim_set_var('one_allow_italics', 1)
vim.api.nvim_set_var('pencil_terminal_italics', 1)
function B16theme(theme)
local base16scheme = b.themes[theme]
b(base16scheme, true)
local colorsfile = vim.fn.stdpath('state') .. '/colorscheme.lua'
local function source_colors()
if vim.fn.filereadable(colorsfile) == 1 then
vim.cmd("source " .. colorsfile)
end
end
-- set the default colorscheme
B16theme('eighties')
-- source colors automatically from colorscheme file on change
local w = vim.loop.new_fs_event()
local function on_change(err, fname, status)
source_colors()
w:stop()
Watch_file(fname)
end
function Watch_file(fname)
local fullpath = vim.api.nvim_call_function('fnamemodify', { fname, ':p' })
w:start(fullpath, {}, vim.schedule_wrap(function(...)
on_change(...)
end))
end
-- set on startup
source_colors()
-- and watch continuously
Watch_file(colorsfile)