diff options
| author | Robert Günzler <r@gnzler.io> | 2024-11-25 13:30:37 +0100 |
|---|---|---|
| committer | Robert Günzler <r@gnzler.io> | 2024-11-25 13:58:25 +0100 |
| commit | 23de12baba9fbfc4ca24581b37c374aa5345cc25 (patch) | |
| tree | e4b3c421bad572eb85a1089eaad44dc3059ab6e6 /.vim/plugin/syncbg.lua | |
| parent | 18e3848110da0a59843058927ea04b496f5db2a5 (diff) | |
update nvim config
Signed-off-by: Robert Günzler <r@gnzler.io>
Diffstat (limited to '.vim/plugin/syncbg.lua')
| -rw-r--r-- | .vim/plugin/syncbg.lua | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/.vim/plugin/syncbg.lua b/.vim/plugin/syncbg.lua new file mode 100644 index 0000000..d9243f0 --- /dev/null +++ b/.vim/plugin/syncbg.lua @@ -0,0 +1,53 @@ +-- this plugin registers two autocmds that trigger when openeing and closing vim, setting the +-- background of the terminal to the color used by our colorscheme using CSI control codes. + +local state = {} + +-- get tty +local tty_handle = io.popen('tty') +if tty_handle == nil then + return +end +local tty = tty_handle:read('*a') +tty_handle:close() +if tty:find('not a tty') then + error('not a tty') +end +state.tty = tty + +-- register update hook +vim.api.nvim_create_autocmd({ 'ColorScheme', 'UIEnter', 'VimResume' }, { + callback = function() + if state.tty == nil then + return + end + + if not state.normal then + -- get colorscheme Normal highlight + local normal = vim.api.nvim_get_hl(0, { name = 'Normal', create = false }) + if normal.bg == nil then + return + end + -- cache old value for use with VimResume + state.normal = normal + end + + -- emit CSI to change terminal background to Normal.bg + os.execute( + 'printf "\\033]11;' .. string.format('#%06x', state.normal.bg) .. '\\007" > ' .. state.tty + ) + + -- set colorscheme Normal.bg to NONE, making it transparent + vim.cmd([[hi Normal ctermbg=NONE guibg=NONE]]) + end, +}) + +-- register reset hook +vim.api.nvim_create_autocmd({ 'UILeave' }, { + callback = function() + if state.tty == nil then + return + end + os.execute('printf "\\033]111\\007" > ' .. state.tty) + end, +}) |