Skip to content

Commit

Permalink
fix: use local variable instead of vim.w, change group name
Browse files Browse the repository at this point in the history
  • Loading branch information
sontungexpt committed Sep 26, 2023
1 parent a8d38e2 commit 0d3f4e9
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lua/stcursorword/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ local new_cmd = api.nvim_create_user_command
local autocmd = api.nvim_create_autocmd
local hl = api.nvim_set_hl

local stcw_group_name = "STCursorword"
local stcw_group_name = "STCursorWord"
local stcw_old_line_pos = -1 -- old line where the word was found
local stcw_old_scol_pos = math.huge -- old start column position of the word found
local stcw_old_ecol_pos = -1 -- old end column position of the word found
Expand Down Expand Up @@ -112,17 +112,19 @@ local is_disabled = function(user_opts)
end

local setup_autocmd = function(user_opts)
hl(0, stcw_group_name, user_opts.highlight) -- make sure highlight option is set when the plugin is loaded or
hl(0, stcw_group_name, user_opts.highlight) -- make sure highlight option is set when the plugin is loaded or reloaded
local group = api.nvim_create_augroup(stcw_group_name, { clear = true })

local is_buf_disabled = nil -- nil means this is the first time entering the buffer

local check_and_matchadd = function()
w.stcw_disabled = (w.stcw_disabled == nil and is_disabled(user_opts)) and true or false
if not w.stcw_disabled then matchadd(user_opts) end
is_buf_disabled = (is_buf_disabled == nil and is_disabled(user_opts)) and true or false
if not is_buf_disabled then matchadd(user_opts) end
end

check_and_matchadd() -- initial match when the plugin is loaded

autocmd({ "ColorScheme" }, { -- make sure highlight is set after ColorScheme
autocmd({ "ColorScheme" }, {
group = group,
callback = function() hl(0, stcw_group_name, user_opts.highlight) end,
})
Expand Down Expand Up @@ -152,7 +154,7 @@ local setup_autocmd = function(user_opts)
group = group,
callback = function()
matchdelete()
w.stcw_disabled = nil
is_buf_disabled = nil
end,
})
g.stcw_enabled = true
Expand Down

0 comments on commit 0d3f4e9

Please # to comment.