diff --git a/lua/orgmode/org/mappings.lua b/lua/orgmode/org/mappings.lua index 52fc770f..38228adb 100644 --- a/lua/orgmode/org/mappings.lua +++ b/lua/orgmode/org/mappings.lua @@ -609,15 +609,7 @@ function OrgMappings:meta_return(suffix) suffix = suffix or '' local item = ts_utils.get_node_at_cursor() - if not item then - return - end - - if item:type() == 'expr' then - item = item:parent() - end - - if item and item:parent() and item:parent():type() == 'headline' then + if item and item:type() == 'expr' then item = item:parent() end @@ -625,12 +617,21 @@ function OrgMappings:meta_return(suffix) return end - if item:type() == 'headline' then - local linenr = vim.fn.line('.') or 0 - local _, level = item:field('stars')[1]:end_() + local headline = (item:type() == 'headline') and item + or (item:parent() and item:parent():type() == 'headline') and item:parent() + or nil + if headline then + local _, level = headline:field('stars')[1]:end_() local content = config:respect_blank_before_new_entry({ ('*'):rep(level) .. ' ' .. suffix }) - vim.fn.append(linenr, content) - vim.fn.cursor(linenr + #content, 1) + + local section = headline:parent() + if not section or section:type() ~= 'section' then + return + end + local end_row = section:end_() + + vim.fn.append(end_row, content) + vim.fn.cursor(end_row + #content, 1) vim.cmd([[startinsert!]]) return true end diff --git a/tests/plenary/ui/mappings/meta_return_spec.lua b/tests/plenary/ui/mappings/meta_return_spec.lua index 8be49701..5424d285 100644 --- a/tests/plenary/ui/mappings/meta_return_spec.lua +++ b/tests/plenary/ui/mappings/meta_return_spec.lua @@ -127,9 +127,9 @@ describe('Meta return mappings', function() vim.cmd([[exe "norm ,\"]]) assert.are.same({ '* DONE top level todo :WORK:', + 'content for top level todo', '', '* ', - 'content for top level todo', '* TODO top level todo with multiple tags :OFFICE:PROJECT:', ' - [ ] The checkbox', }, vim.api.nvim_buf_get_lines(0, 2, 8, false)) @@ -158,8 +158,8 @@ describe('Meta return mappings', function() vim.cmd([[exe "norm ,\"]]) assert.are.same({ '* DONE top level todo :WORK:', - '* ', 'content for top level todo', + '* ', '* TODO top level todo with multiple tags :OFFICE:PROJECT:', ' - [ ] The checkbox', }, vim.api.nvim_buf_get_lines(0, 2, 7, false)) @@ -168,7 +168,7 @@ describe('Meta return mappings', function() }) end) - it('should add headline with Enter right after the current headline (org_meta_return)', function() + it('should add headline with Enter after all the content of the current headline (org_meta_return)', function() helpers.create_agenda_file({ '#TITLE: Test', '', @@ -194,13 +194,13 @@ describe('Meta return mappings', function() vim.cmd([[exe "norm ,\"]]) assert.are.same({ '* TODO Test orgmode', - '', - '* ', ' DEADLINE: <2021-07-21 Wed 22:02>', '** TODO [#A] Test orgmode level 2 :PRIVATE:', 'Some content for level 2', '*** NEXT [#1] Level 3', 'Content Level 3', + '', + '* ', '* DONE top level todo :WORK:', }, vim.api.nvim_buf_get_lines(0, 2, 11, false)) end)