Skip to content

Commit

Permalink
fix(meta_return): insert headline after content of current headline f…
Browse files Browse the repository at this point in the history
…ixes #775
  • Loading branch information
Matthias Bidlingmeyer committed Oct 27, 2024
1 parent c654095 commit 290d6b8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
29 changes: 15 additions & 14 deletions lua/orgmode/org/mappings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -609,28 +609,29 @@ 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

if not item then
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
Expand Down
10 changes: 5 additions & 5 deletions tests/plenary/ui/mappings/meta_return_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ describe('Meta return mappings', function()
vim.cmd([[exe "norm ,\<CR>"]])
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))
Expand Down Expand Up @@ -158,8 +158,8 @@ describe('Meta return mappings', function()
vim.cmd([[exe "norm ,\<CR>"]])
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))
Expand All @@ -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',
'',
Expand All @@ -194,13 +194,13 @@ describe('Meta return mappings', function()
vim.cmd([[exe "norm ,\<CR>"]])
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)
Expand Down

0 comments on commit 290d6b8

Please sign in to comment.