Skip to content

Commit df38874

Browse files
author
Matthias Bidlingmeyer
committed
fix(meta_return): insert headline after content of current headline fixes nvim-orgmode#775
1 parent 8b1dfcd commit df38874

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

lua/orgmode/org/mappings.lua

+15-14
Original file line numberDiff line numberDiff line change
@@ -609,28 +609,29 @@ function OrgMappings:meta_return(suffix)
609609
suffix = suffix or ''
610610
local item = ts_utils.get_node_at_cursor()
611611

612-
if not item then
613-
return
614-
end
615-
616-
if item:type() == 'expr' then
617-
item = item:parent()
618-
end
619-
620-
if item and item:parent() and item:parent():type() == 'headline' then
612+
if item and item:type() == 'expr' then
621613
item = item:parent()
622614
end
623615

624616
if not item then
625617
return
626618
end
627619

628-
if item:type() == 'headline' then
629-
local linenr = vim.fn.line('.') or 0
630-
local _, level = item:field('stars')[1]:end_()
620+
local headline = (item:type() == 'headline') and item
621+
or (item:parent() and item:parent():type() == 'headline') and item:parent()
622+
or nil
623+
if headline then
624+
local _, level = headline:field('stars')[1]:end_()
631625
local content = config:respect_blank_before_new_entry({ ('*'):rep(level) .. ' ' .. suffix })
632-
vim.fn.append(linenr, content)
633-
vim.fn.cursor(linenr + #content, 1)
626+
627+
local section = headline:parent()
628+
if not section or section:type() ~= 'section' then
629+
return
630+
end
631+
local end_row = section:end_()
632+
633+
vim.fn.append(end_row, content)
634+
vim.fn.cursor(end_row + #content, 1)
634635
vim.cmd([[startinsert!]])
635636
return true
636637
end

tests/plenary/ui/mappings/meta_return_spec.lua

+5-5
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,9 @@ describe('Meta return mappings', function()
127127
vim.cmd([[exe "norm ,\<CR>"]])
128128
assert.are.same({
129129
'* DONE top level todo :WORK:',
130+
'content for top level todo',
130131
'',
131132
'* ',
132-
'content for top level todo',
133133
'* TODO top level todo with multiple tags :OFFICE:PROJECT:',
134134
' - [ ] The checkbox',
135135
}, vim.api.nvim_buf_get_lines(0, 2, 8, false))
@@ -158,8 +158,8 @@ describe('Meta return mappings', function()
158158
vim.cmd([[exe "norm ,\<CR>"]])
159159
assert.are.same({
160160
'* DONE top level todo :WORK:',
161-
'* ',
162161
'content for top level todo',
162+
'* ',
163163
'* TODO top level todo with multiple tags :OFFICE:PROJECT:',
164164
' - [ ] The checkbox',
165165
}, vim.api.nvim_buf_get_lines(0, 2, 7, false))
@@ -168,7 +168,7 @@ describe('Meta return mappings', function()
168168
})
169169
end)
170170

171-
it('should add headline with Enter right after the current headline (org_meta_return)', function()
171+
it('should add headline with Enter after all the content of the current headline (org_meta_return)', function()
172172
helpers.create_agenda_file({
173173
'#TITLE: Test',
174174
'',
@@ -194,13 +194,13 @@ describe('Meta return mappings', function()
194194
vim.cmd([[exe "norm ,\<CR>"]])
195195
assert.are.same({
196196
'* TODO Test orgmode',
197-
'',
198-
'* ',
199197
' DEADLINE: <2021-07-21 Wed 22:02>',
200198
'** TODO [#A] Test orgmode level 2 :PRIVATE:',
201199
'Some content for level 2',
202200
'*** NEXT [#1] Level 3',
203201
'Content Level 3',
202+
'',
203+
'* ',
204204
'* DONE top level todo :WORK:',
205205
}, vim.api.nvim_buf_get_lines(0, 2, 11, false))
206206
end)

0 commit comments

Comments
 (0)