diff options
| author | Robert Günzler <r@gnzler.io> | 2025-04-30 13:14:22 +0200 |
|---|---|---|
| committer | Robert Günzler <r@gnzler.io> | 2025-04-30 13:14:22 +0200 |
| commit | 6a1810f4dfe4ac22dfbaf2b98fb7618fd76eb761 (patch) | |
| tree | d9cc1046e3305476e2eea284fbdafde049cf3e10 /.vim/plugin/syncbg.lua | |
| parent | 59bdb9bc3f7b06b886a24def842f62f350f1411b (diff) | |
vim: misc updates
Signed-off-by: Robert Günzler <r@gnzler.io>
Diffstat (limited to '.vim/plugin/syncbg.lua')
| -rw-r--r-- | .vim/plugin/syncbg.lua | 90 |
1 files changed, 55 insertions, 35 deletions
diff --git a/.vim/plugin/syncbg.lua b/.vim/plugin/syncbg.lua index d9243f0..c11bef6 100644 --- a/.vim/plugin/syncbg.lua +++ b/.vim/plugin/syncbg.lua @@ -1,53 +1,73 @@ --- this plugin registers two autocmds that trigger when openeing and closing vim, setting the +-- this plugin registers two autocmds that trigger when openeing and closing the editor, setting the -- background of the terminal to the color used by our colorscheme using CSI control codes. -local state = {} +local state = { enabled = false } --- get tty -local tty_handle = io.popen('tty') -if tty_handle == nil then +if not state.enabled then return end -local tty = tty_handle:read('*a') -tty_handle:close() -if tty:find('not a tty') then - error('not a tty') + +local set_bg = function(clear) + if not (state.tty and state.normal) then + return + end + if clear then + -- emit CSI to unset custom background + os.execute('printf "\\033]111\\007" > ' .. state.tty) + else + -- 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 end -state.tty = tty --- register update hook -vim.api.nvim_create_autocmd({ 'ColorScheme', 'UIEnter', 'VimResume' }, { - callback = function() - if state.tty == nil then +local discover = function() + if not state.tty then + -- get tty handle + local ok, tty_handle = pcall(io.popen, 'tty') + if (not ok) or tty_handle == nil then return end + local tty = tty_handle:read('*l') + tty_handle:close() + if tty:find('not a tty') then + return + end + state.tty = tty + 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 + 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 - ) + -- vim.notify('syncbg: ' .. vim.inspect(state), vim.log.levels.DEBUG) +end - -- set colorscheme Normal.bg to NONE, making it transparent - vim.cmd([[hi Normal ctermbg=NONE guibg=NONE]]) - end, +-- setup autogroup +local aug = vim.api.nvim_create_augroup('syncbg', { clear = true }) + +-- register update hook +vim.api.nvim_create_autocmd({ 'ColorScheme', 'UIEnter', 'VimResume' }, { + group = aug, + callback = vim.schedule_wrap(function() + discover() + set_bg(false) + 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, + group = aug, + callback = vim.schedule_wrap(function() + set_bg(true) + end), }) |