Skip to content

Commit a2bdc7f

Browse files
committed
feat(keybinds): change the way the leader key is passed around and refactor function names to make them more intuitive
Former-commit-id: 712b4ba
1 parent f2234fa commit a2bdc7f

File tree

2 files changed

+38
-22
lines changed

2 files changed

+38
-22
lines changed

lua/neorg/modules/core/keybinds/default_keybinds.lua

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ local module = neorg.modules.extend("core.keybinds.default_keybinds")
33
---@class core.keybinds
44
module.config.public = {
55
keybind_presets = {
6-
neorg = function(keybinds, neorg_leader)
6+
neorg = function(keybinds)
7+
local leader = keybinds.leader
8+
79
-- Map all the below keybinds only when the "norg" mode is active
810
keybinds.map_event_to_mode("norg", {
911
n = { -- Bind keys in normal mode
@@ -19,12 +21,12 @@ module.config.public = {
1921
{ "<C-Space>", "core.norg.qol.todo_items.todo.task_cycle" },
2022

2123
-- Keys for managing GTD
22-
{ neorg_leader .. "tc", "core.gtd.base.capture" },
23-
{ neorg_leader .. "tv", "core.gtd.base.views" },
24-
{ neorg_leader .. "te", "core.gtd.base.edit" },
24+
{ leader .. "tc", "core.gtd.base.capture" },
25+
{ leader .. "tv", "core.gtd.base.views" },
26+
{ leader .. "te", "core.gtd.base.edit" },
2527

2628
-- Keys for managing notes
27-
{ neorg_leader .. "nn", "core.norg.dirman.new.note" },
29+
{ leader .. "nn", "core.norg.dirman.new.note" },
2830

2931
{ "<CR>", "core.norg.esupports.hop.hop-link" },
3032
{ "<M-CR>", "core.norg.esupports.hop.hop-link", "vsplit" },
@@ -33,7 +35,7 @@ module.config.public = {
3335
{ "<M-j>", "core.norg.manoeuvre.item_down" },
3436

3537
-- mnemonic: markup toggle
36-
{ neorg_leader .. "mt", "core.norg.concealer.toggle-markup" },
38+
{ leader .. "mt", "core.norg.concealer.toggle-markup" },
3739

3840
{ "<C-s>", "core.integrations.telescope.find_linkable" },
3941
},
@@ -116,8 +118,8 @@ module.config.public = {
116118
-- Apply the below keys to all modes
117119
keybinds.map_to_mode("all", {
118120
n = {
119-
{ neorg_leader .. "mn", ":Neorg mode norg<CR>" },
120-
{ neorg_leader .. "mh", ":Neorg mode traverse-heading<CR>" },
121+
{ leader .. "mn", ":Neorg mode norg<CR>" },
122+
{ leader .. "mh", ":Neorg mode traverse-heading<CR>" },
121123
},
122124
}, {
123125
silent = true,

lua/neorg/modules/core/keybinds/module.lua

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ every keybind bit by bit.
2424
config = {
2525
hook = function(keybinds)
2626
keybinds.unmap("norg", "n", "gtd")
27-
keybinds.remap("norg", "n", "<C-Space>", "gta")
27+
keybinds.remap_event("norg", "n", "<C-Space>", "core.norg.qol.todo_items.todo.task_done")
2828
end,
2929
}
3030
}
@@ -258,16 +258,30 @@ module.public = {
258258
bound_keys[neorg_mode][mode][key] = bound_keys[neorg_mode][mode][key] and nil
259259
end,
260260

261-
remap = function(neorg_mode, mode, old_key, new_key)
262-
payload.rremap(neorg_mode, mode, old_key, mode, new_key)
261+
remap = function(neorg_mode, mode, key, new_rhs)
262+
local opts = bound_keys[neorg_mode][mode][key].opts
263+
264+
payload.map(neorg_mode, mode, key, new_rhs, opts)
265+
end,
266+
267+
remap_event = function(neorg_mode, mode, key, new_event)
268+
local opts = bound_keys[neorg_mode][mode][key].opts
269+
270+
payload.map(
271+
neorg_mode,
272+
mode,
273+
key,
274+
"<cmd>Neorg keybind " .. neorg_mode .. " " .. new_event .. "<CR>",
275+
opts
276+
)
263277
end,
264278

265-
rremap = function(neorg_mode, mode, old_key, new_mode, new_key)
279+
remap_key = function(neorg_mode, mode, old_key, new_key)
266280
local command = bound_keys[neorg_mode][mode][old_key].command
267281
local opts = bound_keys[neorg_mode][mode][old_key].opts
268282

269283
payload.unmap(neorg_mode, mode, old_key)
270-
payload.map(neorg_mode, new_mode, new_key, command, opts)
284+
payload.map(neorg_mode, mode, new_key, command, opts)
271285
end,
272286

273287
-- @Summary Maps a bunch of keys for a certain mode
@@ -333,21 +347,21 @@ module.public = {
333347
leader = module.config.public.neorg_leader,
334348
}
335349

336-
--- Generates a set of default functions for a given list of them
337-
-- Default functions are postfixed with "d" and omit the beginning "neorg_mode"
338-
-- parameter. The string "norg" is used in those places.
339-
--- @vararg string A list of strings that define the functions to be given a default counterpart
340-
local function generate_default_functions(...)
350+
local function generate_default_functions(cb, ...)
341351
local funcs = { ... }
342352

343353
for _, func in ipairs(funcs) do
344-
payload[func .. "d"] = function(...)
345-
payload[func]("norg", ...)
346-
end
354+
local name, to_exec = cb(func, payload[func])
355+
356+
payload[name] = to_exec
347357
end
348358
end
349359

350-
generate_default_functions("map", "map_event", "unmap", "remap")
360+
generate_default_functions(function(name, func)
361+
return name .. "d", function(...)
362+
func("norg", ...)
363+
end
364+
end, "map", "map_event", "unmap", "remap", "remap_key", "remap_event")
351365

352366
if
353367
module.config.public.default_keybinds

0 commit comments

Comments
 (0)