Skip to content

Commit 94b92b3

Browse files
mintoyaechasnovski
andcommitted
fix(misc): update setup_termbg_sync() to also handle transparent bg
Details: - When a color scheme with transparent background is applied, terminal background sync did nothing. This could result into showing a different terminal bg color that was set by the previous sync. Instead of doing nothing - reset to the original terminal bg color. Resolve #1955 Co-authored-by: Evgeni Chasnovski <evgeni.chasnovski@gmail.com>
1 parent c5f2b25 commit 94b92b3

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

lua/mini/misc.lua

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -319,26 +319,26 @@ MiniMisc.setup_termbg_sync = function()
319319
-- Neovim=0.10 uses string sequence as response, while Neovim>=0.11 sets it
320320
-- in `sequence` table field
321321
local seq = type(args.data) == 'table' and args.data.sequence or args.data
322-
local ok, bg_init = pcall(H.parse_osc11, seq)
323-
if not (ok and type(bg_init) == 'string') then return table.insert(bad_responses, seq) end
322+
local ok, termbg = pcall(H.parse_osc11, seq)
323+
if not (ok and type(termbg) == 'string') then return table.insert(bad_responses, seq) end
324324
had_proper_response = true
325325
pcall(vim.api.nvim_del_autocmd, track_au_id)
326326

327+
-- Set up reset to the color returned from the very first call
328+
H.termbg_init = H.termbg_init or termbg
329+
local reset = function() io.stdout:write('\027]11;' .. H.termbg_init .. '\007') end
330+
vim.api.nvim_create_autocmd({ 'VimLeavePre', 'VimSuspend' }, { group = augroup, callback = reset })
331+
327332
-- Set up sync
328333
local sync = function()
329334
local normal = vim.api.nvim_get_hl_by_name('Normal', true)
330-
if normal.background == nil then return end
335+
if normal.background == nil then return reset() end
331336
-- NOTE: use `io.stdout` instead of `io.write` to ensure correct target
332337
-- Otherwise after `io.output(file); file:close()` there is an error
333338
io.stdout:write(string.format('\027]11;#%06x\007', normal.background))
334339
end
335340
vim.api.nvim_create_autocmd({ 'VimResume', 'ColorScheme' }, { group = augroup, callback = sync })
336341

337-
-- Set up reset to the color returned from the very first call
338-
H.termbg_init = H.termbg_init or bg_init
339-
local reset = function() io.stdout:write('\027]11;' .. H.termbg_init .. '\007') end
340-
vim.api.nvim_create_autocmd({ 'VimLeavePre', 'VimSuspend' }, { group = augroup, callback = reset })
341-
342342
-- Sync immediately
343343
sync()
344344
end

tests/test_misc.lua

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,21 @@ T['setup_termbg_sync()']['handles different color formats'] = function()
676676
validate('rgba:1/23/456/1234', '#112345')
677677
end
678678

679+
T['setup_termbg_sync()']['handles transparent `Normal` background'] = function()
680+
skip_if_no_010()
681+
682+
child.cmd('hi Normal guifg=#222222 guibg=#dddddd')
683+
child.lua('MiniMisc.setup_termbg_sync()')
684+
child.api.nvim_exec_autocmds('TermResponse', { data = '\27]11;rgb:1111/2626/2d2d' })
685+
child.lua('_G.log = {}')
686+
687+
-- When syncing with "transparent" `Normal`, should restore the original
688+
-- terminal background
689+
child.cmd('hi Normal guifg=#222222 guibg=NONE')
690+
child.api.nvim_exec_autocmds('ColorScheme', {})
691+
eq(child.lua_get('_G.log'), { { '\27]11;#11262d\a' } })
692+
end
693+
679694
local restore_cursor_test_file = make_path(dir_misc_path, 'restore-cursor.lua')
680695
local restore_cursor_init_file = make_path(dir_misc_path, 'init-restore-cursor.lua')
681696
local restore_cursor_shada_path = make_path(dir_misc_path, 'restore-cursor.shada')

0 commit comments

Comments
 (0)