Skip to content

Commit

Permalink
fix: set global keybinds only once, set keys on filetype
Browse files Browse the repository at this point in the history
  • Loading branch information
vhyrro committed Jul 15, 2024
1 parent bbe4243 commit e00042a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 30 deletions.
23 changes: 20 additions & 3 deletions lua/neorg/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,35 @@ return {
if key_healthcheck.preset_exists then
vim.health.info(string.format("Neorg is configured to use keybind preset `%s`", keybinds_config.preset))
else
vim.health.error(string.format("Invalid configuration found: preset `%s` does not exist! Did you perhaps make a typo?", keybinds_config.preset))
vim.health.error(
string.format(
"Invalid configuration found: preset `%s` does not exist! Did you perhaps make a typo?",
keybinds_config.preset
)
)
return
end

for remap_key, remap_rhs in vim.spairs(key_healthcheck.remaps) do
vim.health.ok(string.format("Action `%s` (bound to `%s` by default) has been remapped to something else in your configuration.", remap_rhs, remap_key))
vim.health.ok(
string.format(
"Action `%s` (bound to `%s` by default) has been remapped to something else in your configuration.",
remap_rhs,
remap_key
)
)
end

local ok = true

for conflict_key, rhs in vim.spairs(key_healthcheck.conflicts) do
vim.health.warn(string.format("Key `%s` conflicts with a key bound by the user. Neorg will not bind this key.", conflict_key), string.format("consider mapping `%s` to a different key than the one bound by Neorg.", rhs))
vim.health.warn(
string.format(
"Key `%s` conflicts with a key bound by the user. Neorg will not bind this key.",
conflict_key
),
string.format("consider mapping `%s` to a different key than the one bound by Neorg.", rhs)
)
ok = false
end

Expand Down
52 changes: 25 additions & 27 deletions lua/neorg/modules/core/keybinds/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,15 @@ local bound_keys = {}

module.load = function()
if module.config.public.default_keybinds then
vim.api.nvim_create_autocmd("BufEnter", {
local preset = module.private.presets[module.config.public.preset]
assert(preset, string.format("keybind preset `%s` does not exist!", module.config.public.preset))

module.public.set_keys_for(false, preset.all)

vim.api.nvim_create_autocmd("FileType", {
pattern = "norg",
callback = function(ev)
module.public.bind_keys(ev.buf)
module.public.set_keys_for(ev.buf, preset.norg)
end,
})
end
Expand Down Expand Up @@ -111,35 +117,27 @@ module.public = {
end

extend(original_preset, preset)
module.public.bind_keys(vim.api.nvim_get_current_buf())
module.public.bind_norg_keys(vim.api.nvim_get_current_buf())
end,

bind_keys = function(buffer)
local is_norg = vim.bo.filetype == "norg"

local preset = module.private.presets[module.config.public.preset]
assert(preset, string.format("keybind preset `%s` does not exist!", module.config.public.preset))

local function set_keys_for(data)
for mode, keybinds in pairs(data) do
bound_keys[mode] = bound_keys[mode] or {}

for _, keybind in ipairs(keybinds) do
if vim.fn.hasmapto(keybind[2], mode, false) == 0 and vim.fn.mapcheck(keybind[1], mode, false):len() == 0 then
local opts = vim.tbl_deep_extend("force", { buffer = buffer or true }, keybinds.opts or {})
vim.keymap.set(mode, keybind[1], keybind[2], opts)

bound_keys[mode][keybind[1]] = true
end
---@param buffer number|boolean
---@param preset_subdata table
set_keys_for = function(buffer, preset_subdata)
for mode, keybinds in pairs(preset_subdata) do
bound_keys[mode] = bound_keys[mode] or {}

for _, keybind in ipairs(keybinds) do
if
vim.fn.hasmapto(keybind[2], mode, false) == 0
and vim.fn.mapcheck(keybind[1], mode, false):len() == 0
then
local opts = vim.tbl_deep_extend("force", { buffer = buffer }, keybinds.opts or {})
vim.keymap.set(mode, keybind[1], keybind[2], opts)

bound_keys[mode][keybind[1]] = true
end
end
end

set_keys_for(preset.all)

if is_norg then
set_keys_for(preset.norg)
end
end,

--- Checks the health of keybinds. Returns all remaps and all conflicts in a table.
Expand Down Expand Up @@ -178,7 +176,7 @@ module.public = {
remaps = remaps,
conflicts = conflicts,
}
end
end,
}

module.private = {
Expand Down

0 comments on commit e00042a

Please sign in to comment.