Skip to content

Commit 1284458

Browse files
Handle fallback <CR> mapping that is mapped as expression
1 parent de8c5b9 commit 1284458

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

lua/orgmode/config/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ end
176176

177177
function Config:setup_mappings(category)
178178
if not self.old_cr_mapping then
179-
self.old_cr_mapping = vim.fn.maparg('<CR>', 'i')
179+
self.old_cr_mapping = vim.fn.maparg('<CR>', 'i', false, true)
180180
end
181181
if self.opts.mappings.disable_all then
182182
return

lua/orgmode/org/mappings.lua

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -443,12 +443,22 @@ function OrgMappings:org_return()
443443
end
444444
end
445445

446-
local fallback_mapping = '<CR>'
447-
if config.old_cr_mapping and config.old_cr_mapping ~= '' then
448-
fallback_mapping = config.old_cr_mapping
446+
if not config.old_cr_mapping or vim.tbl_isempty(config.old_cr_mapping) then
447+
return vim.api.nvim_feedkeys(utils.esc('<CR>'), 'n', true)
449448
end
450449

451-
return vim.api.nvim_feedkeys(utils.esc(fallback_mapping), 'n', true)
450+
local rhs = config.old_cr_mapping.rhs
451+
452+
if config.old_cr_mapping.expr > 0 then
453+
vim.api.nvim_feedkeys(utils.esc(string.format('<C-r>=%s<CR>', rhs)), 'n', true)
454+
-- Echo empty message to remove the register feedkeys output
455+
vim.schedule(function()
456+
vim.cmd([[echo '']])
457+
end)
458+
return
459+
end
460+
461+
return vim.api.nvim_feedkeys(utils.esc(rhs), 'n', true)
452462
end
453463

454464
function OrgMappings:handle_return(suffix)

tests/plenary/ui/table_spec.lua

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,28 @@ describe('Tables', function()
5353
'| fifth | sixth | seventh |',
5454
}, vim.api.nvim_buf_get_lines(0, 0, -1, false))
5555
end)
56+
57+
it('should add new row on enter', function()
58+
helpers.load_file_content({
59+
'| test |',
60+
})
61+
vim.fn.cursor({ 1, 6 })
62+
vim.cmd([[exe "norm a\<CR>"]])
63+
assert.are.same({
64+
'| test |',
65+
'| |',
66+
}, vim.api.nvim_buf_get_lines(0, 0, -1, false))
67+
68+
helpers.load_file_content({
69+
'| test | col |',
70+
'| | value',
71+
})
72+
vim.fn.cursor({ 2, 13 })
73+
vim.cmd([[exe "norm a\<CR>"]])
74+
assert.are.same({
75+
'| test | col |',
76+
'| | value |',
77+
'| | |',
78+
}, vim.api.nvim_buf_get_lines(0, 0, -1, false))
79+
end)
5680
end)

0 commit comments

Comments
 (0)