diff --git a/lua/neorg/modules/core/autocommands/module.lua b/lua/neorg/modules/core/autocommands/module.lua index 5cae451de..babdc896d 100644 --- a/lua/neorg/modules/core/autocommands/module.lua +++ b/lua/neorg/modules/core/autocommands/module.lua @@ -68,11 +68,12 @@ module.public = { autocmd = autocmd:lower() local subscribed_autocommand = module.events.subscribed["core.autocommands"][autocmd] - if subscribed_autocommand ~= nil then - vim.cmd("augroup Neorg") + local group = vim.api.nvim_create_augroup("Neorg", { clear = false }) + if subscribed_autocommand ~= nil then if dont_isolate and vim.fn.exists("#Neorg#" .. autocmd .. "#*") == 0 then vim.api.nvim_create_autocmd(autocmd, { + group = group, callback = function(ev) _neorg_module_autocommand_triggered("core.autocommands.events." .. autocmd, false, ev) end, @@ -80,12 +81,12 @@ module.public = { elseif vim.fn.exists("#Neorg#" .. autocmd .. "#*.norg") == 0 then vim.api.nvim_create_autocmd(autocmd, { pattern = "*.norg", + group = group, callback = function(ev) _neorg_module_autocommand_triggered("core.autocommands.events." .. autocmd, true, ev) end, }) end - vim.cmd("augroup END") module.events.subscribed["core.autocommands"][autocmd] = true end end, diff --git a/lua/neorg/modules/core/esupports/indent/module.lua b/lua/neorg/modules/core/esupports/indent/module.lua index 795e9b599..4e88b4b07 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,30 @@ 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() + if event.line_content == "" then + return + end + + local lineno_1b = event.cursor_position[1] + local old_indent = vim.fn.indent(lineno_1b) + local new_indent = module.public.indentexpr(0, lineno_1b - 1) + if old_indent ~= new_indent then + vim.bo.undolevels = vim.bo.undolevels + vim.api.nvim_buf_set_text(0, lineno_1b-1, 0, lineno_1b-1, old_indent, { (" "):rep(new_indent) }) + end + end) + end end end module.events.subscribed = { ["core.autocommands"] = { bufenter = true, + insertleave = true, }, } diff --git a/lua/neorg/modules/core/journal/module.lua b/lua/neorg/modules/core/journal/module.lua index 17a78bbfd..0af56890b 100644 --- a/lua/neorg/modules/core/journal/module.lua +++ b/lua/neorg/modules/core/journal/module.lua @@ -135,8 +135,6 @@ module.private = { module.required["core.dirman"].create_file(folder_name .. config.pathsep .. path, workspace) - module.required["core.dirman"].create_file(folder_name .. config.pathsep .. path, workspace) - if not journal_file_exists and module.config.public.use_template