From d1d80f08858ff3355531a853bc3a89df18ca4780 Mon Sep 17 00:00:00 2001 From: Fitrah Muhammad Date: Wed, 7 Sep 2022 14:18:01 +0800 Subject: [PATCH 1/5] fix: use key to check visual mode cb54e80 introduce changes to check visual mode highlight using `current_mode`, this changes causes a bug with `Shift + v` (visual line mode) where the cursor highlight still uses the default `Visual` highlight until pressing the second key. Checking visual mode highlights Using `key` (and checking it inside `current_mode == 'n'`) is more reliable than using `current_mode`. --- lua/modes.lua | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/lua/modes.lua b/lua/modes.lua index 0b009da..7ebef6d 100644 --- a/lua/modes.lua +++ b/lua/modes.lua @@ -221,22 +221,24 @@ M.setup = function(opts) operator_started = true return end - end - if - current_mode:lower() == 'v' - or current_mode == utils.replace_termcodes('') - then if - key == utils.replace_termcodes('') - or key == current_mode + key:lower() == 'v' + or key == utils.replace_termcodes('') then - M.reset() - else M.highlight('visual') operator_started = true + return end end + + if + key == utils.replace_termcodes('') + or key == current_mode + then + M.reset() + return + end end end) From 5bf70bb2d0a66e12c402327f5fa2a056b9c279a9 Mon Sep 17 00:00:00 2001 From: Fitrah Muhammad Date: Wed, 7 Sep 2022 14:38:39 +0800 Subject: [PATCH 2/5] early return --- lua/modes.lua | 70 +++++++++++++++++++++++++-------------------------- 1 file changed, 34 insertions(+), 36 deletions(-) diff --git a/lua/modes.lua b/lua/modes.lua index 7ebef6d..7917271 100644 --- a/lua/modes.lua +++ b/lua/modes.lua @@ -195,51 +195,49 @@ 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('') then - M.reset() - return - end + return + end + + if current_mode == 'i' then + if key == utils.replace_termcodes('') 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 - key:lower() == 'v' - or key == utils.replace_termcodes('') - then - M.highlight('visual') - 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 key == 'y' then + M.highlight('copy') + operator_started = true + return + end + + if key == 'd' then + M.highlight('delete') + operator_started = true + return end if - key == utils.replace_termcodes('') - or key == current_mode + key:lower() == 'v' + or key == utils.replace_termcodes('') then - M.reset() + M.highlight('visual') + operator_started = true return end end + + if key == utils.replace_termcodes('') or key == current_mode then + M.reset() + return + end end) ---Set highlights when colorscheme changes From 6c527bc500c3bf1d7c797c801399a32ec0e5948b Mon Sep 17 00:00:00 2001 From: Fitrah Muhammad Date: Thu, 8 Sep 2022 13:16:02 +0800 Subject: [PATCH 3/5] fix: reset highlight on WinNew event Fix behaviour when creating new split windows using `CTRL-W_v` where modes.nvim treats `v` keypress as visual mode. --- lua/modes.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/modes.lua b/lua/modes.lua index 7917271..3966ce9 100644 --- a/lua/modes.lua +++ b/lua/modes.lua @@ -256,7 +256,7 @@ M.setup = function(opts) ---Reset highlights vim.api.nvim_create_autocmd( - { 'CmdlineLeave', 'InsertLeave', 'TextYankPost', 'WinLeave' }, + { 'CmdlineLeave', 'InsertLeave', 'TextYankPost', 'WinNew', 'WinLeave' }, { pattern = '*', callback = M.reset, From eca72df4917dd672eb9990bea1e1583ce193e818 Mon Sep 17 00:00:00 2001 From: Fitrah Muhammad Date: Thu, 8 Sep 2022 14:47:52 +0800 Subject: [PATCH 4/5] feat: handle visual highlight using ModeChanged event --- lua/modes.lua | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/lua/modes.lua b/lua/modes.lua index 3966ce9..6c65c60 100644 --- a/lua/modes.lua +++ b/lua/modes.lua @@ -223,18 +223,9 @@ M.setup = function(opts) operator_started = true return end - - if - key:lower() == 'v' - or key == utils.replace_termcodes('') - then - M.highlight('visual') - operator_started = true - return - end end - if key == utils.replace_termcodes('') or key == current_mode then + if key == utils.replace_termcodes('') then M.reset() return end @@ -254,9 +245,17 @@ M.setup = function(opts) end, }) + ---Set visual highlight + vim.api.nvim_create_autocmd('ModeChanged', { + pattern = '*:[vV\x16]', + callback = function() + M.highlight('visual') + end, + }) + ---Reset highlights vim.api.nvim_create_autocmd( - { 'CmdlineLeave', 'InsertLeave', 'TextYankPost', 'WinNew', 'WinLeave' }, + { 'CmdlineLeave', 'InsertLeave', 'TextYankPost', 'WinLeave' }, { pattern = '*', callback = M.reset, From 377a8006dd1329ad2f52c25fb5cb4ba700859127 Mon Sep 17 00:00:00 2001 From: Fitrah Muhammad Date: Thu, 8 Sep 2022 18:06:05 +0800 Subject: [PATCH 5/5] fix: add autocmd to reset visual highlight This handles the case where leaving visual mode without `` or `vim.on_key` doesn't catch mode switching like toggling comments in visual mode. --- lua/modes.lua | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lua/modes.lua b/lua/modes.lua index 6c65c60..efc25bc 100644 --- a/lua/modes.lua +++ b/lua/modes.lua @@ -253,6 +253,12 @@ M.setup = function(opts) 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' },