From 25d37d17b375585d6ba4de2b388a909bdaf1f96f Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Fri, 3 Mar 2023 18:35:21 +0100 Subject: [PATCH] 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. --- nvim/.config/nvim/lua/look.lua | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/nvim/.config/nvim/lua/look.lua b/nvim/.config/nvim/lua/look.lua index 88c94fb..801980e 100644 --- a/nvim/.config/nvim/lua/look.lua +++ b/nvim/.config/nvim/lua/look.lua @@ -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)