Skip to content

Commit 5ac150c

Browse files
authored
fix(mappings): properly get old mapping (#554)
* fix(mappings): properly get old mapping * fix: add missing buffer * chore: add annotations
1 parent c340aab commit 5ac150c

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

lua/orgmode/config/init.lua

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,17 @@ function Config:get_todo_keywords()
260260
return types
261261
end
262262

263-
function Config:setup_mappings(category, bufnr)
263+
--- Setup mappings for a given category and buffer
264+
---@param category string Mapping category name (e.g. `agenda`, `capture`, `node`)
265+
---@param buffer number? Buffer id
266+
---@see orgmode.config.mappings
267+
function Config:setup_mappings(category, buffer)
264268
if not self.old_cr_mapping then
265-
self.old_cr_mapping = vim.fn.maparg('<CR>', 'i', false, true)
269+
self.old_cr_mapping = utils.get_keymap({
270+
mode = 'i',
271+
lhs = '<CR>',
272+
buffer = buffer,
273+
})
266274
end
267275
if self.opts.mappings.disable_all then
268276
return
@@ -272,8 +280,8 @@ function Config:setup_mappings(category, bufnr)
272280
local default_mappings = defaults.mappings[category] or {}
273281
local user_mappings = vim.tbl_get(self.opts.mappings, category) or {}
274282
local opts = {}
275-
if bufnr then
276-
opts.buffer = bufnr
283+
if buffer then
284+
opts.buffer = buffer
277285
end
278286

279287
if self.opts.mappings.prefix then

lua/orgmode/utils/init.lua

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,28 @@ function utils.menu(title, items, prompt)
167167
return entry.action()
168168
end
169169

170+
---@class KeymapData
171+
---@field mode string|table
172+
---@field lhs string
173+
---@field buffer integer?
174+
175+
---@param data KeymapData
176+
---@return table? map Mapping definition
177+
function utils.get_keymap(data)
178+
local keymaps
179+
if data.buffer then
180+
keymaps = vim.api.nvim_buf_get_keymap(data.buffer, data.mode)
181+
else
182+
keymaps = vim.api.nvim_get_keymap(data.mode)
183+
end
184+
185+
for _, map in ipairs(keymaps) do
186+
if map.lhs == data.lhs then
187+
return map
188+
end
189+
end
190+
end
191+
170192
function utils.esc(cmd)
171193
return vim.api.nvim_replace_termcodes(cmd, true, false, true)
172194
end

0 commit comments

Comments
 (0)