diff --git a/lua/orgmode/org/mappings.lua b/lua/orgmode/org/mappings.lua index 52fc770f..8f6b34b7 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 @@ -730,10 +731,8 @@ function OrgMappings:insert_heading_respect_content(suffix) if not item then self:_insert_heading_from_plain_line(suffix) else - local line = config:respect_blank_before_new_entry({ string.rep('*', item:get_level()) .. ' ' .. suffix }) - local end_line = item:get_range().end_line - vim.fn.append(end_line, line) - vim.fn.cursor(end_line + #line, 1) + vim.fn.cursor(item:get_range().start_line, 1) + return self:meta_return(suffix) end return vim.cmd([[startinsert!]]) end @@ -744,13 +743,16 @@ end function OrgMappings:insert_todo_heading() local item = self.files:get_closest_headline_or_nil() - local first_todo_keyword = config:get_todo_keywords():first_by_type('TODO') + local first_todo_keyword = config:get_todo_keywords():first_by_type('TODO').value .. ' ' if not item then - self:_insert_heading_from_plain_line(first_todo_keyword.value .. ' ') + self:_insert_heading_from_plain_line(first_todo_keyword) return vim.cmd([[startinsert!]]) else - vim.fn.cursor(item:get_range().start_line, 1) - return self:meta_return(first_todo_keyword.value .. ' ') + local level = string.rep('*', item:get_level()) .. ' ' + local line = config:respect_blank_before_new_entry({ level .. first_todo_keyword }) + local start_line = item:get_range().start_line + vim.fn.append(start_line, line) + vim.fn.cursor(start_line + #line, 1) end 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)