Skip to content

Commit f32a06d

Browse files
fix(mappings): Handle <CR> fallback
1 parent 4f99b37 commit f32a06d

1 file changed

Lines changed: 21 additions & 10 deletions

File tree

lua/orgmode/org/mappings.lua

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -630,24 +630,35 @@ function OrgMappings:org_return()
630630
mode = 'i',
631631
lhs = '<CR>',
632632
})
633-
-- No other mapping for <CR>, just reproduce it.
633+
634634
if not global_cr_keymap or vim.tbl_isempty(global_cr_keymap) then
635635
return vim.api.nvim_feedkeys(utils.esc('<CR>'), 'n', true)
636636
end
637637

638-
local rhs = global_cr_keymap.rhs
638+
local function get_rhs()
639+
if global_cr_keymap.callback then
640+
local result = global_cr_keymap.callback()
641+
if global_cr_keymap.expr == 0 or not result then
642+
return
643+
end
644+
return vim.api.nvim_replace_termcodes(result, true, true, true)
645+
end
646+
647+
if global_cr_keymap.expr > 0 then
648+
-- expr rhs: the string is a Vimscript expression, eval it first
649+
local ok, result = pcall(vim.api.nvim_eval, global_cr_keymap.rhs)
650+
if ok then
651+
return vim.api.nvim_replace_termcodes(result, true, true, true)
652+
end
653+
end
639654

640-
if global_cr_keymap.callback then
641-
rhs = global_cr_keymap.callback()
655+
return vim.api.nvim_replace_termcodes(global_cr_keymap.rhs, true, true, true)
642656
end
643657

644-
-- If mapping contains `\r`, it means it's already escaped and evaluated
645-
if global_cr_keymap.expr > 0 and not rhs:lower():find('\r') then
646-
rhs = vim.api.nvim_replace_termcodes(rhs, true, true, true)
647-
rhs = vim.api.nvim_eval(rhs)
658+
local rhs = get_rhs()
659+
if rhs then
660+
return vim.api.nvim_feedkeys(rhs, 'n', true)
648661
end
649-
650-
return vim.api.nvim_feedkeys(rhs, 'n', true)
651662
end
652663

653664
function OrgMappings:handle_return(suffix)

0 commit comments

Comments
 (0)