Skip to content

Commit

Permalink
save cursor position when changing vim.wo.cc
Browse files Browse the repository at this point in the history
  • Loading branch information
ribru17 committed Mar 31, 2023
1 parent 80b6cd4 commit 7eca7b1
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lua/deadcolumn/autocmds.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ local function redraw_colorcolumn()
-- Only set vim.wo.cc when necessary,
-- prevent cursor position from being reset.
if vim.wo.cc ~= '' then
local winview = vim.fn.winsaveview()
vim.wo.cc = ''
vim.fn.winrestview(winview)
end
return
end
Expand All @@ -92,13 +94,17 @@ local function redraw_colorcolumn()
len < thresh or not vim.tbl_contains(configs.user.modes, vim.fn.mode())
then
if vim.wo.cc ~= '' then
local winview = vim.fn.winsaveview()
vim.wo.cc = ''
vim.fn.winrestview(winview)
end
return
end

if vim.wo.cc ~= vim.w.cc then
local winview = vim.fn.winsaveview()
vim.wo.cc = vim.w.cc
vim.fn.winrestview(winview)
end

-- Show blended color when len < cc
Expand Down Expand Up @@ -158,7 +164,9 @@ local function make_autocmds()
group = 'AutoColorColumn',
callback = function()
store.previous_cc = vim.w.cc
local winview = vim.fn.winsaveview()
vim.wo.cc = ''
vim.fn.winrestview(winview)
end,
})

Expand Down Expand Up @@ -221,7 +229,9 @@ local function make_autocmds()
vim.api.nvim_create_autocmd({ 'BufLeave' }, {
group = 'AutoColorColumn',
callback = function()
local winview = vim.fn.winsaveview()
vim.wo.cc = ''
vim.fn.winrestview(winview)
end,
})
vim.api.nvim_create_autocmd({ 'BufWinEnter' }, {
Expand All @@ -230,7 +240,9 @@ local function make_autocmds()
vim.b.cc = str_fallback(vim.wo.cc, vim.b.cc, vim.g.cc)
vim.w.cc = str_fallback(vim.wo.cc, vim.b.cc, vim.g.cc)
if not vim.tbl_contains(configs.user.modes, vim.fn.mode()) then
local winview = vim.fn.winsaveview()
vim.wo.cc = ''
vim.fn.winrestview(winview)
end
end,
})
Expand All @@ -257,7 +269,9 @@ local function make_autocmds()
vim.b.cc = vim.wo.cc
end
vim.go.cc = ''
local winview = vim.fn.winsaveview()
vim.wo.cc = ''
vim.fn.winrestview(winview)
end,
})

Expand Down

0 comments on commit 7eca7b1

Please sign in to comment.