Skip to content

use function callbacks for keybinds and commands #1063

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
83 changes: 37 additions & 46 deletions lua/neorg/modules/core/journal/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -428,17 +428,47 @@ module.load = function()
min_args = 1,
max_args = 2,
subcommands = {
tomorrow = { args = 0, name = "journal.tomorrow" },
yesterday = { args = 0, name = "journal.yesterday" },
today = { args = 0, name = "journal.today" },
custom = { max_args = 1, name = "journal.custom" }, -- format :yyyy-mm-dd
template = { args = 0, name = "journal.template" },
tomorrow = { args = 0, name = "journal.tomorrow", callback = module.private.diary_tomorrow },
yesterday = { args = 0, name = "journal.yesterday", callback = module.private.diary_yesterday },
today = { args = 0, name = "journal.today", callback = module.private.diary_today },
custom = {
max_args = 1,
name = "journal.custom",
callback = function(args)
if not args or not args[1] then
local calendar = modules.get_module("core.ui.calendar")

if not calendar then
log.error(
"[ERROR]: `core.ui.calendar` is not loaded! Said module is required for this operation."
)
return
end

calendar.select_date({
callback = vim.schedule_wrap(function(osdate)
module.private.open_diary(
nil,
string.format("%04d", osdate.year)
.. "-"
.. string.format("%02d", osdate.month)
.. "-"
.. string.format("%02d", osdate.day)
)
end),
})
else
module.private.open_diary(nil, args[1])
end
end,
}, -- format :yyyy-mm-dd
template = { args = 0, name = "journal.template", callback = module.private.create_template },
toc = {
args = 1,
name = "journal.toc",
subcommands = {
open = { args = 0, name = "journal.toc.open" },
update = { args = 0, name = "journal.toc.update" },
open = { args = 0, name = "journal.toc.open", callback = module.private.open_toc },
update = { args = 0, name = "journal.toc.update", callback = module.private.create_toc },
},
},
},
Expand All @@ -447,45 +477,6 @@ module.load = function()
end

module.on_event = function(event)
if vim.tbl_contains({ "core.keybinds", "core.neorgcmd" }, event.split_type[1]) then
if event.split_type[2] == "journal.tomorrow" then
module.private.diary_tomorrow()
elseif event.split_type[2] == "journal.yesterday" then
module.private.diary_yesterday()
elseif event.split_type[2] == "journal.custom" then
if not event.content[1] then
local calendar = modules.get_module("core.ui.calendar")

if not calendar then
log.error("[ERROR]: `core.ui.calendar` is not loaded! Said module is required for this operation.")
return
end

calendar.select_date({
callback = vim.schedule_wrap(function(osdate)
module.private.open_diary(
nil,
string.format("%04d", osdate.year)
.. "-"
.. string.format("%02d", osdate.month)
.. "-"
.. string.format("%02d", osdate.day)
)
end),
})
else
module.private.open_diary(nil, event.content[1])
end
elseif event.split_type[2] == "journal.today" then
module.private.diary_today()
elseif event.split_type[2] == "journal.template" then
module.private.create_template()
elseif event.split_type[2] == "journal.toc.open" then
module.private.open_toc()
elseif event.split_type[2] == "journal.toc.update" then
module.private.create_toc()
end
end
end

module.events.subscribed = {
Expand Down
4 changes: 4 additions & 0 deletions lua/neorg/modules/core/neorgcmd/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,10 @@ module.private = {
module.events.defined[ref.name] = modules.define_event(module, ref.name)
end

if ref.callback then
ref.callback(vim.list_slice(args, argument_index + 1))
end

modules.broadcast_event(
modules.create_event(
module,
Expand Down