Skip to content

Commit

Permalink
feat: handle visual highlight using ModeChanged event (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
fitrh authored Oct 10, 2022
1 parent 7abb725 commit 73791bf
Showing 1 changed file with 42 additions and 37 deletions.
79 changes: 42 additions & 37 deletions lua/modes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -195,48 +195,39 @@ M.setup = function(opts)
local ok, current_mode = pcall(vim.fn.mode)
if not ok then
M.reset()
else
if current_mode == 'i' then
if key == utils.replace_termcodes('<esc>') then
M.reset()
return
end
return
end

if current_mode == 'i' then
if key == utils.replace_termcodes('<esc>') then
M.reset()
return
end
end

if current_mode == 'n' then
-- reset if coming back from operator pending mode
if operator_started then
M.reset()
return
end

if key == 'y' then
M.highlight('copy')
operator_started = true
return
end

if key == 'd' then
M.highlight('delete')
operator_started = true
return
end
if current_mode == 'n' then
-- reset if coming back from operator pending mode
if operator_started then
M.reset()
return
end

if
current_mode:lower() == 'v'
or current_mode == utils.replace_termcodes('<c-v>')
then
if
key == utils.replace_termcodes('<esc>')
or key == current_mode
then
M.reset()
else
M.highlight('visual')
operator_started = true
end
if key == 'y' then
M.highlight('copy')
operator_started = true
return
end

if key == 'd' then
M.highlight('delete')
operator_started = true
return
end
end

if key == utils.replace_termcodes('<esc>') then
M.reset()
return
end
end)

Expand All @@ -254,6 +245,20 @@ M.setup = function(opts)
end,
})

---Set visual highlight
vim.api.nvim_create_autocmd('ModeChanged', {
pattern = '*:[vV\x16]',
callback = function()
M.highlight('visual')
end,
})

---Reset visual highlight
vim.api.nvim_create_autocmd('ModeChanged', {
pattern = '[vV\x16]:n',
callback = M.reset,
})

---Reset highlights
vim.api.nvim_create_autocmd(
{ 'CmdlineLeave', 'InsertLeave', 'TextYankPost', 'WinLeave' },
Expand Down

0 comments on commit 73791bf

Please sign in to comment.