Skip to content

Commit 6566b69

Browse files
fix(mappings): Correctly fall back to previously defined <CR> mapping
This fixes expansion of brackets and similar things.
1 parent 7092f81 commit 6566b69

File tree

2 files changed

+11
-24
lines changed

2 files changed

+11
-24
lines changed

lua/orgmode/config/init.lua

-10
Original file line numberDiff line numberDiff line change
@@ -211,16 +211,6 @@ end
211211
---@param buffer number? Buffer id
212212
---@see orgmode.config.mappings
213213
function Config:setup_mappings(category, buffer)
214-
if category == 'org' and vim.bo.filetype == 'org' and not vim.b[buffer].org_old_cr_mapping then
215-
local old_mapping = utils.get_keymap({
216-
mode = 'i',
217-
lhs = '<CR>',
218-
buffer = buffer,
219-
})
220-
if old_mapping and old_mapping.rhs ~= '<Cmd>lua require("orgmode").action("org_mappings.org_return")<CR>' then
221-
vim.b[buffer].org_old_cr_mapping = old_mapping
222-
end
223-
end
224214
local maps = self:get_mappings(category, buffer)
225215
if not maps then
226216
return

lua/orgmode/org/mappings.lua

+11-14
Original file line numberDiff line numberDiff line change
@@ -571,30 +571,27 @@ function OrgMappings:org_return()
571571
end
572572
end
573573

574-
local old_mapping = vim.b.org_old_cr_mapping
575-
574+
local global_cr_keymap = utils.get_keymap({
575+
mode = 'i',
576+
lhs = '<CR>',
577+
})
576578
-- No other mapping for <CR>, just reproduce it.
577-
if not old_mapping or vim.tbl_isempty(old_mapping) then
579+
if not global_cr_keymap or vim.tbl_isempty(global_cr_keymap) then
578580
return vim.api.nvim_feedkeys(utils.esc('<CR>'), 'n', true)
579581
end
580582

581-
local rhs = old_mapping.rhs
582-
local eval = old_mapping.expr > 0
583+
local rhs = global_cr_keymap.rhs
583584

584-
if old_mapping.callback then
585-
rhs = old_mapping.callback()
586-
eval = false
585+
if global_cr_keymap.callback then
586+
rhs = global_cr_keymap.callback()
587587
end
588588

589-
if eval then
589+
-- If mapping contains `\r`, it means it's already escaped and evaluated
590+
if global_cr_keymap.expr > 0 and not rhs:lower():find('\r') then
591+
rhs = vim.api.nvim_replace_termcodes(rhs, true, true, true)
590592
rhs = vim.api.nvim_eval(rhs)
591593
end
592594

593-
-- If the rhs is empty, assume that callback already handled the action
594-
if old_mapping.callback and not rhs then
595-
return
596-
end
597-
598595
return vim.api.nvim_feedkeys(rhs, 'n', true)
599596
end
600597

0 commit comments

Comments
 (0)