Skip to content

Commit e00042a

Browse files
committed
fix: set global keybinds only once, set keys on filetype
1 parent bbe4243 commit e00042a

File tree

2 files changed

+45
-30
lines changed

2 files changed

+45
-30
lines changed

lua/neorg/health.lua

+20-3
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,35 @@ return {
9090
if key_healthcheck.preset_exists then
9191
vim.health.info(string.format("Neorg is configured to use keybind preset `%s`", keybinds_config.preset))
9292
else
93-
vim.health.error(string.format("Invalid configuration found: preset `%s` does not exist! Did you perhaps make a typo?", keybinds_config.preset))
93+
vim.health.error(
94+
string.format(
95+
"Invalid configuration found: preset `%s` does not exist! Did you perhaps make a typo?",
96+
keybinds_config.preset
97+
)
98+
)
9499
return
95100
end
96101

97102
for remap_key, remap_rhs in vim.spairs(key_healthcheck.remaps) do
98-
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))
103+
vim.health.ok(
104+
string.format(
105+
"Action `%s` (bound to `%s` by default) has been remapped to something else in your configuration.",
106+
remap_rhs,
107+
remap_key
108+
)
109+
)
99110
end
100111

101112
local ok = true
102113

103114
for conflict_key, rhs in vim.spairs(key_healthcheck.conflicts) do
104-
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))
115+
vim.health.warn(
116+
string.format(
117+
"Key `%s` conflicts with a key bound by the user. Neorg will not bind this key.",
118+
conflict_key
119+
),
120+
string.format("consider mapping `%s` to a different key than the one bound by Neorg.", rhs)
121+
)
105122
ok = false
106123
end
107124

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

+25-27
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,15 @@ local bound_keys = {}
7171

7272
module.load = function()
7373
if module.config.public.default_keybinds then
74-
vim.api.nvim_create_autocmd("BufEnter", {
74+
local preset = module.private.presets[module.config.public.preset]
75+
assert(preset, string.format("keybind preset `%s` does not exist!", module.config.public.preset))
76+
77+
module.public.set_keys_for(false, preset.all)
78+
79+
vim.api.nvim_create_autocmd("FileType", {
80+
pattern = "norg",
7581
callback = function(ev)
76-
module.public.bind_keys(ev.buf)
82+
module.public.set_keys_for(ev.buf, preset.norg)
7783
end,
7884
})
7985
end
@@ -111,35 +117,27 @@ module.public = {
111117
end
112118

113119
extend(original_preset, preset)
114-
module.public.bind_keys(vim.api.nvim_get_current_buf())
120+
module.public.bind_norg_keys(vim.api.nvim_get_current_buf())
115121
end,
116122

117-
bind_keys = function(buffer)
118-
local is_norg = vim.bo.filetype == "norg"
119-
120-
local preset = module.private.presets[module.config.public.preset]
121-
assert(preset, string.format("keybind preset `%s` does not exist!", module.config.public.preset))
122-
123-
local function set_keys_for(data)
124-
for mode, keybinds in pairs(data) do
125-
bound_keys[mode] = bound_keys[mode] or {}
126-
127-
for _, keybind in ipairs(keybinds) do
128-
if vim.fn.hasmapto(keybind[2], mode, false) == 0 and vim.fn.mapcheck(keybind[1], mode, false):len() == 0 then
129-
local opts = vim.tbl_deep_extend("force", { buffer = buffer or true }, keybinds.opts or {})
130-
vim.keymap.set(mode, keybind[1], keybind[2], opts)
131-
132-
bound_keys[mode][keybind[1]] = true
133-
end
123+
---@param buffer number|boolean
124+
---@param preset_subdata table
125+
set_keys_for = function(buffer, preset_subdata)
126+
for mode, keybinds in pairs(preset_subdata) do
127+
bound_keys[mode] = bound_keys[mode] or {}
128+
129+
for _, keybind in ipairs(keybinds) do
130+
if
131+
vim.fn.hasmapto(keybind[2], mode, false) == 0
132+
and vim.fn.mapcheck(keybind[1], mode, false):len() == 0
133+
then
134+
local opts = vim.tbl_deep_extend("force", { buffer = buffer }, keybinds.opts or {})
135+
vim.keymap.set(mode, keybind[1], keybind[2], opts)
136+
137+
bound_keys[mode][keybind[1]] = true
134138
end
135139
end
136140
end
137-
138-
set_keys_for(preset.all)
139-
140-
if is_norg then
141-
set_keys_for(preset.norg)
142-
end
143141
end,
144142

145143
--- Checks the health of keybinds. Returns all remaps and all conflicts in a table.
@@ -178,7 +176,7 @@ module.public = {
178176
remaps = remaps,
179177
conflicts = conflicts,
180178
}
181-
end
179+
end,
182180
}
183181

184182
module.private = {

0 commit comments

Comments
 (0)