Skip to content
Open
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
15 changes: 12 additions & 3 deletions lua/neorg/core/modules.lua
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ local utils = require("neorg.core.utils")
--- @field required? neorg.module.resolver Contains the public tables of all modules that were required via the `requires` array provided in the `setup()` function of this module.
--- @field setup? fun(): neorg.module.setup? Function that is invoked before any other loading occurs. Should perform preliminary startup tasks.
--- @field replaced? boolean If `true`, this means the module is a replacement for a core module. This flag is set automatically whenever `setup().replaces` is set to a value.
--- @field on_event fun(event: neorg.event) A callback that is invoked any time an event the module has subscribed to has fired.
--- @field on_event? fun(event: neorg.event) A callback that is invoked any time an event the module has subscribed to has fired.
--- @field event_callbacks? table<string, table<string, fun(event: neorg.event)>> A callback that is invoked any time an event the module has subscribed to has fired.

---@class neorg.modules
local modules = {}
Expand Down Expand Up @@ -818,7 +819,11 @@ function modules.broadcast_event(event, callback)

if evt ~= nil and evt == true then
-- Run the on_event() for that module
current_module.on_event(event)
if current_module.event_callbacks then
current_module.event_callbacks[event.split_type[1]][event.split_type[2]](event)
else
current_module.on_event(event)
end
end
end
end
Expand Down Expand Up @@ -854,7 +859,11 @@ function modules.send_event(recipient, event)

-- If it is then trigger the module's on_event() function
if evt ~= nil and evt == true then
mod.on_event(event)
if mod.event_callbacks then
mod.event_callbacks[event.split_type[1]][event.split_type[2]](event)
else
mod.on_event(event)
end
end
end
end
Expand Down
34 changes: 20 additions & 14 deletions lua/neorg/modules/core/esupports/indent/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -360,20 +360,26 @@ module.load = function()
ts = module.required["core.integrations.treesitter"]
end

module.on_event = function(event)
if event.type == "core.autocommands.events.bufenter" and event.content.norg then
vim.api.nvim_set_option_value(
"indentexpr",
("v:lua.require'neorg'.modules.get_module('core.esupports.indent').indentexpr(%d)"):format(event.buffer),
{ buf = event.buffer }
)

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_set_option_value("indentkeys", indentkeys, { buf = event.buffer })
end
end
module.event_callbacks = {
["core.autocommands"] = {
bufenter = function(event)
if event.content.norg then
vim.api.nvim_set_option_value(
"indentexpr",
("v:lua.require'neorg'.modules.get_module('core.esupports.indent').indentexpr(%d)"):format(
event.buffer
),
{ buf = event.buffer }
)

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_set_option_value("indentkeys", indentkeys, { buf = event.buffer })
end
end,
},
}

module.events.subscribed = {
["core.autocommands"] = {
Expand Down
211 changes: 115 additions & 96 deletions lua/neorg/modules/core/export/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -274,126 +274,145 @@ module.private = {
end,
}

---@param event neorg.event
module.on_event = function(event)
if event.type == "core.neorgcmd.events.export.to-file" then
-- Syntax: Neorg export to-file file.extension forced-filetype?
-- Example: Neorg export to-file my-custom-file markdown

local filepath = vim.fn.expand(event.content[1])
local filetype = event.content[2] or vim.filetype.match({ filename = filepath })
local exported = module.public.export(event.buffer, filetype)

vim.loop.fs_open(filepath, "w", 438, function(err, fd)
assert(not err, lib.lazy_string_concat("Failed to open file '", filepath, "' for export: ", err))
assert(fd)

vim.loop.fs_write(fd, exported, 0, function(werr)
assert(not werr, lib.lazy_string_concat("Failed to write to file '", filepath, "' for export: ", werr))
module.event_callbacks = {
["core.neorgcmd"] = {
["export.to-file"] = function(event)
-- Syntax: Neorg export to-file file.extension forced-filetype?
-- Example: Neorg export to-file my-custom-file markdown

local filepath = vim.fn.expand(event.content[1])
local filetype = event.content[2] or vim.filetype.match({ filename = filepath })
local exported = module.public.export(event.buffer, filetype)

vim.loop.fs_open(filepath, "w", 438, function(err, fd)
assert(not err, lib.lazy_string_concat("Failed to open file '", filepath, "' for export: ", err))
assert(fd)

vim.loop.fs_write(fd, exported, 0, function(werr)
assert(
not werr,
lib.lazy_string_concat("Failed to write to file '", filepath, "' for export: ", werr)
)
end)

vim.schedule(lib.wrap(utils.notify, "Successfully exported 1 file!"))
end)
vim.api.nvim_exec_autocmds("User", {
pattern = "NeorgExportComplete",
})
end,
["export.to-clipboard"] = function(event)
-- Syntax: Neorg export to-clipboard filetype
-- Example: Neorg export to-clipboard markdown

local filetype = event.content[1]
local data = event.content.data
local exported = module.public.export_range(event.buffer, data.line1, data.line2, filetype)

vim.fn.setreg("+", exported, "l")
vim.api.nvim_exec_autocmds("User", {
pattern = "NeorgExportComplete",
})
end,
["export.directory"] = function(event)
local path = event.content[3] and vim.fn.expand(event.content[3])
or module.config.public.export_dir
:gsub("<language>", event.content[2])
:gsub("<export%-dir>", event.content[1])
vim.fn.mkdir(path, "p")

-- The old value of `eventignore` is stored here. This is done because the eventignore
-- value is set to ignore BufEnter events before loading all the Neorg buffers, as they can mistakenly
-- activate the concealer, which not only slows down performance notably but also causes errors.
---@diagnostic disable-next-line: undefined-field
local old_event_ignore = table.concat(vim.opt.eventignore:get(), ",")

vim.loop.fs_scandir(event.content[1], function(err, handle)
assert(not err, lib.lazy_string_concat("Failed to scan directory '", event.content[1], "': ", err))
assert(handle)

local file_counter, parsed_counter = 0, 0

while true do
local name, type = vim.loop.fs_scandir_next(handle)

if not name then
break
end

vim.schedule(lib.wrap(utils.notify, "Successfully exported 1 file!"))
end)
elseif event.type == "core.neorgcmd.events.export.to-clipboard" then
-- Syntax: Neorg export to-clipboard filetype
-- Example: Neorg export to-clipboard markdown

local filetype = event.content[1]
local data = event.content.data
local exported = module.public.export_range(event.buffer, data.line1, data.line2, filetype)

vim.fn.setreg("+", exported, "l")
elseif event.type == "core.neorgcmd.events.export.directory" then
local path = event.content[3] and vim.fn.expand(event.content[3])
or module.config.public.export_dir
:gsub("<language>", event.content[2])
:gsub("<export%-dir>", event.content[1])
vim.fn.mkdir(path, "p")

-- The old value of `eventignore` is stored here. This is done because the eventignore
-- value is set to ignore BufEnter events before loading all the Neorg buffers, as they can mistakenly
-- activate the concealer, which not only slows down performance notably but also causes errors.
---@diagnostic disable-next-line: undefined-field
local old_event_ignore = table.concat(vim.opt.eventignore:get(), ",")

vim.loop.fs_scandir(event.content[1], function(err, handle)
assert(not err, lib.lazy_string_concat("Failed to scan directory '", event.content[1], "': ", err))
assert(handle)

local file_counter, parsed_counter = 0, 0

while true do
local name, type = vim.loop.fs_scandir_next(handle)

if not name then
break
end

if type == "file" and vim.endswith(name, ".norg") then
file_counter = file_counter + 1
if type == "file" and vim.endswith(name, ".norg") then
file_counter = file_counter + 1

local function check_counters()
parsed_counter = parsed_counter + 1
local function check_counters()
parsed_counter = parsed_counter + 1

if parsed_counter >= file_counter then
vim.schedule(
lib.wrap(utils.notify, string.format("Successfully exported %d files!", file_counter))
)
if parsed_counter >= file_counter then
vim.schedule(
lib.wrap(
utils.notify,
string.format("Successfully exported %d files!", file_counter)
)
)
end
end
end

vim.schedule(function()
local filepath = vim.fn.expand(event.content[1]) .. "/" .. name
vim.schedule(function()
local filepath = vim.fn.expand(event.content[1]) .. "/" .. name

vim.opt.eventignore = "BufEnter"
vim.opt.eventignore = "BufEnter"

local buffer = assert(vim.fn.bufadd(filepath))
vim.fn.bufload(buffer)
local buffer = assert(vim.fn.bufadd(filepath))
vim.fn.bufload(buffer)

vim.opt.eventignore = old_event_ignore
vim.opt.eventignore = old_event_ignore

local exported, extension = module.public.export(buffer, event.content[2])
local exported, extension = module.public.export(buffer, event.content[2])

vim.api.nvim_buf_delete(buffer, { force = true })
vim.api.nvim_buf_delete(buffer, { force = true })

if not exported then
check_counters()
return
end

local write_path = path .. "/" .. name:gsub("%.%a+$", "." .. extension)
if not exported then
check_counters()
return
end

vim.loop.fs_open(write_path, "w+", 438, function(fs_err, fd)
assert(
not fs_err,
lib.lazy_string_concat("Failed to open file '", write_path, "' for export: ", fs_err)
)
assert(fd)
local write_path = path .. "/" .. name:gsub("%.%a+$", "." .. extension)

vim.loop.fs_write(fd, exported, 0, function(werr)
vim.loop.fs_open(write_path, "w+", 438, function(fs_err, fd)
assert(
not werr,
not fs_err,
lib.lazy_string_concat(
"Failed to write to file '",
"Failed to open file '",
write_path,
"' for export: ",
werr
fs_err
)
)
assert(fd)

vim.loop.fs_write(fd, exported, 0, function(werr)
assert(
not werr,
lib.lazy_string_concat(
"Failed to write to file '",
write_path,
"' for export: ",
werr
)
)

check_counters()
check_counters()
end)
end)
end)
end)
end
end
end
end)
end

vim.api.nvim_exec_autocmds("User", {
pattern = "NeorgExportComplete",
})
end
end)
vim.api.nvim_exec_autocmds("User", {
pattern = "NeorgExportComplete",
})
end,
},
}

module.events.subscribed = {
["core.neorgcmd"] = {
Expand Down
24 changes: 10 additions & 14 deletions lua/neorg/modules/core/integrations/otter/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,16 @@ module.events.subscribed = {
},
}

module.on_event = function(event)
if module.private[event.split_type[2]] then
module.private[event.split_type[2]](event)
end
end
module.event_callbacks = {
["core.neorgcmd"] = {
["otter.enable"] = function()
module.public.activate()
end,
["otter.disable"] = function()
module.public.deactivate()
end,
},
}

module.config.public = {
-- list of languages that otter will try to start a language server for.
Expand All @@ -122,15 +127,6 @@ module.config.public = {
},
}

module.private = {
["otter.enable"] = function(_)
module.public.activate()
end,
["otter.disable"] = function(_)
module.public.deactivate()
end,
}

module.public = {
---Activate otter in the current buffer, includes setting buffer keymaps
activate = function()
Expand Down
8 changes: 4 additions & 4 deletions lua/neorg/modules/core/integrations/treesitter/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -992,13 +992,13 @@ module.public.get_next_node = function(node, allow_switch_parents, allow_next_pa
return destination_node
end

module.on_event = function(event)
if event.split_type[2] == "sync-parsers" then
module.event_callbacks = {
["core.neorgcmd"]= {["sync-parsers"] = function()
local install = require("nvim-treesitter.install")
install.commands.TSInstallSync["run!"]("norg")
install.commands.TSInstallSync["run!"]("norg_meta")
end
end
end}
}

module.events.subscribed = {
["core.neorgcmd"] = {
Expand Down
Loading
Loading