Skip to content

Commit

Permalink
fix(autocommands): pass correct buffer id
Browse files Browse the repository at this point in the history
  • Loading branch information
champignoom authored and vhyrro committed Dec 26, 2023
1 parent e35bf90 commit 941119d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
14 changes: 9 additions & 5 deletions lua/neorg/core/modules.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand All @@ -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
Expand Down
30 changes: 14 additions & 16 deletions lua/neorg/modules/core/autocommands/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 941119d

Please sign in to comment.