Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix autocommands, allow undo indent #1260

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lua/neorg/modules/core/autocommands/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,25 @@ 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,
})
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,
Expand Down
25 changes: 23 additions & 2 deletions lua/neorg/modules/core/esupports/indent/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -315,14 +320,30 @@ module.on_event = function(event)

local indentkeys = "o,O,*<M-o>,*<M-O>"
.. lib.when(module.config.public.format_on_enter, ",*<CR>", "")
.. lib.when(module.config.public.format_on_escape, ",*<Esc>", "")
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,
},
}

Expand Down
2 changes: 0 additions & 2 deletions lua/neorg/modules/core/journal/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading