Skip to content

Commit c0584ec

Browse files
SlayerOfTheBadJacob Schoemaker
andauthored
bug(core) Ignore invisible headings (#773)
Previously, `next-visible-heading`, moved to the next heading, regardless of its visibility. Co-authored-by: Jacob Schoemaker <[email protected]>
1 parent 85c1b44 commit c0584ec

File tree

2 files changed

+116
-4
lines changed

2 files changed

+116
-4
lines changed

lua/orgmode/org/mappings.lua

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -924,12 +924,26 @@ function OrgMappings:export()
924924
return require('orgmode.export').prompt()
925925
end
926926

927+
---Find and move cursor to next visible heading.
928+
---@return integer
927929
function OrgMappings:next_visible_heading()
928-
return vim.fn.search([[^\*\+]], 'W')
930+
return vim.fn.search([[^\*\+]], 'W', 0, 0, self._skip_invisible_heading)
929931
end
930932

933+
---Find and move cursor to previous visible heading.
934+
---@return integer
931935
function OrgMappings:previous_visible_heading()
932-
return vim.fn.search([[^\*\+]], 'bW')
936+
return vim.fn.search([[^\*\+]], 'bW', 0, 0, self._skip_invisible_heading)
937+
end
938+
939+
---Check if heading is visible. If not, skip it.
940+
---@return integer
941+
function OrgMappings:_skip_invisible_heading()
942+
local fold = vim.fn.foldclosed('.')
943+
if fold == -1 or vim.fn.line('.') == fold then
944+
return 0
945+
end
946+
return 1
933947
end
934948

935949
function OrgMappings:forward_heading_same_level()

tests/plenary/ui/mappings/headline_spec.lua

Lines changed: 100 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ describe('Heading mappings', function()
498498
' 2. Second item',
499499
})
500500

501-
vim.cmd([[norm gg]])
501+
vim.cmd([[norm ggzR]])
502502
assert.are.same(1, vim.fn.line('.'))
503503
vim.cmd([[norm }]])
504504
assert.are.same(3, vim.fn.line('.'))
@@ -514,6 +514,53 @@ describe('Heading mappings', function()
514514
assert.are.same(16, vim.fn.line('.'))
515515
end)
516516

517+
it('should jump to next visible heading on any level (org_next_visible_heading)', function()
518+
helpers.create_file({
519+
'#TITLE: Test',
520+
'',
521+
'* TODO Test orgmode',
522+
' DEADLINE: <2021-07-21 Wed 22:02>',
523+
'** TODO [#A] Test orgmode level 2 :PRIVATE:',
524+
'Some content for level 2',
525+
'*** NEXT [#1] Level 3',
526+
'Content Level 3',
527+
'* DONE top level todo :WORK:',
528+
'content for top level todo',
529+
'* TODO top level todo with multiple tags :OFFICE:PROJECT:',
530+
' - [ ] The checkbox',
531+
' - [X] The checkbox 2',
532+
' - [ ] Nested checkbox',
533+
'multiple tags content, tags not read from content :FROMCONTENT:',
534+
'** NEXT Working on this now :OFFICE:NESTED:',
535+
' 1. First item',
536+
' 2. Second item',
537+
})
538+
539+
-- Default collapsed
540+
vim.cmd([[norm gg]])
541+
assert.are.same(1, vim.fn.line('.'))
542+
vim.cmd([[norm }]])
543+
assert.are.same(3, vim.fn.line('.'))
544+
vim.cmd([[norm }]])
545+
assert.are.same(9, vim.fn.line('.'))
546+
vim.cmd([[norm }]])
547+
assert.are.same(11, vim.fn.line('.'))
548+
549+
-- Open first level
550+
vim.cmd([[norm ggzr]])
551+
assert.are.same(1, vim.fn.line('.'))
552+
vim.cmd([[norm }]])
553+
assert.are.same(3, vim.fn.line('.'))
554+
vim.cmd([[norm }]])
555+
assert.are.same(5, vim.fn.line('.'))
556+
vim.cmd([[norm }]])
557+
assert.are.same(9, vim.fn.line('.'))
558+
vim.cmd([[norm }]])
559+
assert.are.same(11, vim.fn.line('.'))
560+
vim.cmd([[norm }]])
561+
assert.are.same(16, vim.fn.line('.'))
562+
end)
563+
517564
it('should jump to previous heading on any level (org_previous_visible_heading)', function()
518565
helpers.create_file({
519566
'#TITLE: Test',
@@ -536,7 +583,7 @@ describe('Heading mappings', function()
536583
' 2. Second item',
537584
})
538585

539-
vim.cmd([[norm G]])
586+
vim.cmd([[norm GzR]])
540587
assert.are.same(18, vim.fn.line('.'))
541588
vim.cmd([[norm {]])
542589
assert.are.same(16, vim.fn.line('.'))
@@ -554,6 +601,57 @@ describe('Heading mappings', function()
554601
assert.are.same(3, vim.fn.line('.'))
555602
end)
556603

604+
it('should jump to previous visible heading on any level (org_previous_visible_heading)', function()
605+
helpers.create_file({
606+
'#TITLE: Test',
607+
'',
608+
'* TODO Test orgmode',
609+
' DEADLINE: <2021-07-21 Wed 22:02>',
610+
'** TODO [#A] Test orgmode level 2 :PRIVATE:',
611+
'Some content for level 2',
612+
'*** NEXT [#1] Level 3',
613+
'Content Level 3',
614+
'* DONE top level todo :WORK:',
615+
'content for top level todo',
616+
'* TODO top level todo with multiple tags :OFFICE:PROJECT:',
617+
' - [ ] The checkbox',
618+
' - [X] The checkbox 2',
619+
' - [ ] Nested checkbox',
620+
'multiple tags content, tags not read from content :FROMCONTENT:',
621+
'** NEXT Working on this now :OFFICE:NESTED:',
622+
' 1. First item',
623+
' 2. Second item',
624+
})
625+
626+
-- Default to only top level
627+
vim.cmd([[norm G]])
628+
assert.are.same(18, vim.fn.line('.'))
629+
vim.cmd([[norm {]])
630+
assert.are.same(11, vim.fn.line('.'))
631+
vim.cmd([[norm {]])
632+
assert.are.same(9, vim.fn.line('.'))
633+
vim.cmd([[norm {]])
634+
assert.are.same(3, vim.fn.line('.'))
635+
vim.cmd([[norm {]])
636+
assert.are.same(3, vim.fn.line('.'))
637+
638+
-- open one dir
639+
vim.cmd([[exec "norm Gzr"]])
640+
assert.are.same(18, vim.fn.line('.'))
641+
vim.cmd([[norm {]])
642+
assert.are.same(16, vim.fn.line('.'))
643+
vim.cmd([[norm {]])
644+
assert.are.same(11, vim.fn.line('.'))
645+
vim.cmd([[norm {]])
646+
assert.are.same(9, vim.fn.line('.'))
647+
vim.cmd([[norm {]])
648+
assert.are.same(5, vim.fn.line('.'))
649+
vim.cmd([[norm {]])
650+
assert.are.same(3, vim.fn.line('.'))
651+
vim.cmd([[norm {]])
652+
assert.are.same(3, vim.fn.line('.'))
653+
end)
654+
557655
it('should jump to next heading on same level (org_backward_heading_same_level)', function()
558656
helpers.create_file({
559657
'#TITLE: Test',

0 commit comments

Comments
 (0)