diff --git a/lua/neorg/health.lua b/lua/neorg/health.lua index 812cc5d6c..3394fd02f 100644 --- a/lua/neorg/health.lua +++ b/lua/neorg/health.lua @@ -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 diff --git a/lua/neorg/modules/core/keybinds/module.lua b/lua/neorg/modules/core/keybinds/module.lua index 71d249041..89cb285f0 100644 --- a/lua/neorg/modules/core/keybinds/module.lua +++ b/lua/neorg/modules/core/keybinds/module.lua @@ -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 @@ -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. @@ -178,7 +176,7 @@ module.public = { remaps = remaps, conflicts = conflicts, } - end + end, } module.private = {