Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(meta_return): insert headline after content of current headline #822

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 24 additions & 22 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 Expand Up @@ -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
Expand All @@ -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

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
Loading