Skip to content

Commit 491e66e

Browse files
committed
feat: add enable() and disable()
1 parent e349636 commit 491e66e

4 files changed

Lines changed: 57 additions & 18 deletions

File tree

lua/modicator/backup.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
local M = {}
2+
3+
--- @type table
4+
local cursor_line_nr_hl_backup = nil
5+
6+
function M.backup_default_cursorline_hl()
7+
local hl = require('modicator.utils').get_highlight('CursorLineNr')
8+
cursor_line_nr_hl_backup = hl
9+
end
10+
11+
function M.restore_default_cursorline_hl()
12+
if cursor_line_nr_hl_backup then
13+
vim.api.nvim_set_hl(0, 'CursorLineNr', cursor_line_nr_hl_backup)
14+
end
15+
end
16+
17+
return M

lua/modicator/init.lua

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@ local options = {
3333
},
3434
}
3535

36+
---@type integer?
37+
local augroup_id = nil
38+
3639
--- @return ModicatorOptions
37-
M.get_options = function()
40+
function M.get_options()
3841
return options
3942
end
4043

@@ -115,7 +118,7 @@ local function mode_name_from_mode(mode)
115118
end
116119

117120
--- @param mode string
118-
M.hl_name_from_mode = function(mode)
121+
function M.hl_name_from_mode(mode)
119122
local mode_name = mode_name_from_mode(mode)
120123
return mode_name .. 'Mode'
121124
end
@@ -183,10 +186,9 @@ local function set_highlight_groups()
183186
update_mode()
184187
end
185188

186-
--- Set the foreground and background color of 'CursorLineNr'. Accepts any
187-
--- highlight definition map that `vim.api.nvim_set_hl()` does.
188-
--- @param hl_name string
189-
M.set_cursor_line_highlight = function(hl_name)
189+
--- Set the foreground and background color of 'CursorLineNr'
190+
--- @param hl_name string Name of mode highlight group
191+
function M.set_cursor_line_highlight(hl_name)
190192
local hl_group = require('modicator.utils').get_highlight(hl_name)
191193
local hl = vim.tbl_extend('force', options.highlights.defaults, hl_group)
192194
if options.highlights.use_cursorline_background == true then
@@ -195,14 +197,15 @@ M.set_cursor_line_highlight = function(hl_name)
195197
end
196198
api.nvim_set_hl(0, 'CursorLineNr', hl)
197199

198-
local is_register_executing = vim.fn.reg_executing() ~= ""
200+
local register_is_executing = vim.fn.reg_executing() ~= ""
199201

200202
-- Workaround for https://github.com/neovim/neovim/issues/25851
201-
if not vim.o.lazyredraw and not is_register_executing then
203+
if not vim.o.lazyredraw and not register_is_executing then
202204
vim.cmd.redraw()
203205
end
204206
end
205207

208+
---@return integer augroup Augroup ID
206209
local function create_autocmds()
207210
local augroup = api.nvim_create_augroup('Modicator', {})
208211
-- NOTE: VimEnter loads after user's configuration is loaded
@@ -221,17 +224,36 @@ local function create_autocmds()
221224
callback = set_highlight_groups,
222225
group = augroup,
223226
})
227+
228+
return augroup
229+
end
230+
231+
--- Enable Modicator
232+
function M.enable()
233+
set_highlight_groups()
234+
235+
api.nvim_set_hl(0, 'CursorLineNr', { link = 'NormalMode' })
236+
237+
augroup_id = create_autocmds()
238+
end
239+
240+
--- Disable Modicator
241+
function M.disable()
242+
if augroup_id then
243+
api.nvim_del_augroup_by_id(augroup_id)
244+
augroup_id = nil
245+
end
246+
247+
require('modicator.backup').restore_default_cursorline_hl()
224248
end
225249

226250
--- @param opts ModicatorOptions?
227251
function M.setup(opts)
228252
options = vim.tbl_deep_extend('force', options, opts or {})
229253

230-
set_highlight_groups()
231-
232-
vim.api.nvim_set_hl(0, 'CursorLineNr', { link = 'NormalMode' })
254+
require('modicator.backup').backup_default_cursorline_hl()
233255

234-
create_autocmds()
256+
M.enable()
235257
end
236258

237259
return M

lua/modicator/integration/lualine/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ end
100100

101101
--- Set mode highlights based on lualine's mode highlights
102102
--- @param mode_section LualineSectionLetter?
103-
M.use_lualine_mode_highlights = function(mode_section)
103+
function M.use_lualine_mode_highlights(mode_section)
104104
mode_section = mode_section or get_mode_section_letter()
105105
-- If lualine doesn't have a `mode` section and none was passed in
106106
if mode_section == nil then

lua/modicator/utils.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ local modicator = require('modicator')
33
local M = {}
44

55
--- @param message string
6-
M.warn = function(message)
6+
function M.warn(message)
77
if modicator.get_options().show_warnings then
88
local warning = string.format('modicator.nvim: %s', message)
99
vim.notify(warning, vim.log.levels.WARN)
1010
end
1111
end
1212

1313
--- @param message string
14-
M.inform = function(message)
14+
function M.inform(message)
1515
if modicator.get_options().show_warnings then
1616
local warning = string.format('modicator.nvim: %s', message)
1717
vim.notify(warning, vim.log.levels.INFO)
@@ -20,13 +20,13 @@ end
2020

2121
--- @param hl_group string
2222
--- @return boolean
23-
M.highlight_exists = function(hl_group)
23+
function M.highlight_exists(hl_group)
2424
local hl = vim.api.nvim_get_hl(0, { name = hl_group })
2525
return not vim.tbl_isempty(hl)
2626
end
2727

2828
--- @return table<string>
29-
M.get_highlights = function()
29+
function M.get_highlights()
3030
local modes = require('modicator').modes
3131
return vim.tbl_map(function(mode)
3232
return mode .. 'Mode'
@@ -36,7 +36,7 @@ end
3636
--- Gets the highlight `group`.
3737
--- @param hl_name string
3838
--- @return table<string, any>
39-
M.get_highlight = function(hl_name)
39+
function M.get_highlight(hl_name)
4040
return vim.api.nvim_get_hl(0, { name = hl_name, link = false })
4141
end
4242

0 commit comments

Comments
 (0)