diff --git a/lua/neorg/core/modules.lua b/lua/neorg/core/modules.lua index 693d58600..1660250b6 100644 --- a/lua/neorg/core/modules.lua +++ b/lua/neorg/core/modules.lua @@ -649,8 +649,9 @@ end ---@param module table #A reference to the module invoking the function ---@param type string #A full path to a valid event type (e.g. 'core.module.events.some_event') ---@param content any #The content of the event, can be anything from a string to a table to whatever you please +---@param ev? table the original event data ---@return table #New event -function modules.create_event(module, type, content) +function modules.create_event(module, type, content, ev) -- Get the module that contains the event local module_name = modules.split_event_type(type)[1] @@ -673,12 +674,15 @@ function modules.create_event(module, type, content) new_event.split_type = modules.split_event_type(type) new_event.filename = vim.fn.expand("%:t") new_event.filehead = vim.fn.expand("%:p:h") - new_event.cursor_position = vim.api.nvim_win_get_cursor(0) - new_event.line_content = vim.api.nvim_get_current_line() + local bufid = ev and ev.buf or vim.api.nvim_get_current_buf() + local winid = vim.fn.bufwinid(bufid) + new_event.cursor_position = vim.api.nvim_win_get_cursor(winid) + local row_1b = new_event.cursor_position[1] + new_event.line_content = vim.api.nvim_buf_get_lines(bufid, row_1b-1, row_1b, true)[1] new_event.referrer = module.name new_event.broadcast = true - new_event.buffer = vim.api.nvim_get_current_buf() - new_event.window = vim.api.nvim_get_current_win() + new_event.buffer = bufid + new_event.window = winid new_event.mode = vim.api.nvim_get_mode().mode return new_event diff --git a/lua/neorg/modules/core/autocommands/module.lua b/lua/neorg/modules/core/autocommands/module.lua index 6cd015b47..962d200e7 100644 --- a/lua/neorg/modules/core/autocommands/module.lua +++ b/lua/neorg/modules/core/autocommands/module.lua @@ -44,8 +44,9 @@ local module = modules.create("core.autocommands") --- This function gets invoked whenever a core.autocommands enabled autocommand is triggered. Note that this function should be only used internally ---@param name string #The name of the autocommand that was just triggered ---@param triggered_from_norg boolean #If true, that means we have received this event as part of a *.norg autocommand -function _neorg_module_autocommand_triggered(name, triggered_from_norg) - local event = modules.create_event(module, name, { norg = triggered_from_norg }) +---@param ev? table the original event data +function _neorg_module_autocommand_triggered(name, triggered_from_norg, ev) + local event = modules.create_event(module, name, { norg = triggered_from_norg }, ev) assert(event) modules.broadcast_event(event) end @@ -71,21 +72,18 @@ module.public = { vim.cmd("augroup Neorg") if dont_isolate and vim.fn.exists("#Neorg#" .. autocmd .. "#*") == 0 then - vim.cmd( - "autocmd " - .. autocmd - .. ' * :lua _neorg_module_autocommand_triggered("core.autocommands.events.' - .. autocmd - .. '", false)' - ) + vim.api.nvim_create_autocmd(autocmd, { + callback = function(ev) + _neorg_module_autocommand_triggered("core.autocommands.events."..autocmd, false, ev) + end + }) elseif vim.fn.exists("#Neorg#" .. autocmd .. "#*.norg") == 0 then - vim.cmd( - "autocmd " - .. autocmd - .. ' *.norg :lua _neorg_module_autocommand_triggered("core.autocommands.events.' - .. autocmd - .. '", true)' - ) + vim.api.nvim_create_autocmd(autocmd, { + pattern = "*.norg", + 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