diff --git a/lua/neorg/modules/core/esupports/indent/module.lua b/lua/neorg/modules/core/esupports/indent/module.lua index 795e9b5991..f74db55290 100644 --- a/lua/neorg/modules/core/esupports/indent/module.lua +++ b/lua/neorg/modules/core/esupports/indent/module.lua @@ -303,10 +303,15 @@ module.config.public = { module.load = function() module.required["core.autocommands"].enable_autocommand("BufEnter") + module.required["core.autocommands"].enable_autocommand("InsertLeave") end module.on_event = function(event) - if event.type == "core.autocommands.events.bufenter" and event.content.norg then + if not event.content.norg then + return + end + + if event.type == "core.autocommands.events.bufenter" then vim.api.nvim_buf_set_option( event.buffer, "indentexpr", @@ -315,14 +320,21 @@ module.on_event = function(event) local indentkeys = "o,O,*,*" .. lib.when(module.config.public.format_on_enter, ",*", "") - .. lib.when(module.config.public.format_on_escape, ",*", "") vim.api.nvim_buf_set_option(event.buffer, "indentkeys", indentkeys) + elseif event.type == "core.autocommands.events.insertleave" then + if module.config.public.format_on_escape then + vim.api.nvim_buf_call(event.buffer, function() + vim.bo.undolevels = vim.bo.undolevels + vim.cmd("normal! ==") + end) + end end end module.events.subscribed = { ["core.autocommands"] = { bufenter = true, + insertleave = true, }, }