+" Return the line number of the parent header of line `line`.
+"
+" If it has no parent, return `0`.
+"
+function! s:GetParentHeaderLineNumber(...)
+ if a:0 == 0
+ let l:line = line('.')
+ else
+ let l:line = a:1
+ endif
+ let l:level = s:GetHeaderLevel(l:line)
+ if l:level > 1
+ let l:linenum = s:GetPreviousHeaderLineNumberAtLevel(l:level - 1, l:line)
+ return l:linenum
+ endif
+ return 0
+endfunction
+
+" Return the line number of the previous header of given level.
+" in relation to line `a:1`. If not given, `a:1 = getline()`
+"
+" `a:1` line is included, and this may return the current header.
+"
+" If none return 0.
+"
+function! s:GetNextHeaderLineNumberAtLevel(level, ...)
+ if a:0 < 1
+ let l:line = line('.')
+ else
+ let l:line = a:1
+ endif
+ let l:l = l:line
+ while(l:l <= line('$'))
+ if join(getline(l:l, l:l + 1), "\n") =~ get(s:levelRegexpDict, a:level)
+ return l:l
+ endif
+ let l:l += 1
+ endwhile
+ return 0
+endfunction
+
+" Return the line number of the previous header of given level.
+" in relation to line `a:1`. If not given, `a:1 = getline()`
+"
+" `a:1` line is included, and this may return the current header.
+"
+" If none return 0.
+"
+function! s:GetPreviousHeaderLineNumberAtLevel(level, ...)
+ if a:0 == 0
+ let l:line = line('.')
+ else
+ let l:line = a:1
+ endif
+ let l:l = l:line
+ while(l:l > 0)
+ if join(getline(l:l, l:l + 1), "\n") =~ get(s:levelRegexpDict, a:level)
+ return l:l
+ endif
+ let l:l -= 1
+ endwhile
+ return 0
+endfunction
+
+" Move cursor to next sibling header.
+"
+" If there is no next siblings, print a warning and don't move.
+"
+function! s:MoveToNextSiblingHeader()
+ let l:curHeaderLineNumber = s:GetHeaderLineNum()
+ let l:curHeaderLevel = s:GetLevelOfHeaderAtLine(l:curHeaderLineNumber)
+ let l:curHeaderParentLineNumber = s:GetParentHeaderLineNumber()
+ let l:nextHeaderSameLevelLineNumber = s:GetNextHeaderLineNumberAtLevel(l:curHeaderLevel, l:curHeaderLineNumber + 1)
+ let l:noNextSibling = 0
+ if l:nextHeaderSameLevelLineNumber == 0
+ let l:noNextSibling = 1
+ else
+ let l:nextHeaderSameLevelParentLineNumber = s:GetParentHeaderLineNumber(l:nextHeaderSameLevelLineNumber)
+ if l:curHeaderParentLineNumber == l:nextHeaderSameLevelParentLineNumber
+ call cursor(l:nextHeaderSameLevelLineNumber, 1)
+ else
+ let l:noNextSibling = 1
+ endif
+ endif
+ if l:noNextSibling
+ echo 'no next sibling header'
+ endif
+endfunction
+
+" Move cursor to previous sibling header.
+"
+" If there is no previous siblings, print a warning and do nothing.
+"
+function! s:MoveToPreviousSiblingHeader()
+ let l:curHeaderLineNumber = s:GetHeaderLineNum()
+ let l:curHeaderLevel = s:GetLevelOfHeaderAtLine(l:curHeaderLineNumber)
+ let l:curHeaderParentLineNumber = s:GetParentHeaderLineNumber()
+ let l:previousHeaderSameLevelLineNumber = s:GetPreviousHeaderLineNumberAtLevel(l:curHeaderLevel, l:curHeaderLineNumber - 1)
+ let l:noPreviousSibling = 0
+ if l:previousHeaderSameLevelLineNumber == 0
+ let l:noPreviousSibling = 1
+ else
+ let l:previousHeaderSameLevelParentLineNumber = s:GetParentHeaderLineNumber(l:previousHeaderSameLevelLineNumber)
+ if l:curHeaderParentLineNumber == l:previousHeaderSameLevelParentLineNumber
+ call cursor(l:previousHeaderSameLevelLineNumber, 1)
+ else
+ let l:noPreviousSibling = 1
+ endif
+ endif
+ if l:noPreviousSibling
+ echo 'no previous sibling header'
+ endif
+endfunction
+
+function! s:Toc(...)
+ if a:0 > 0
+ let l:window_type = a:1
+ else
+ let l:window_type = 'vertical'
+ endif
+
+ try
+ silent lvimgrep /\(^\S.*\(\n[=-]\+\n\)\@=\|^#\+\)/ %
+ catch /E480/
+ echom "Toc: No headers."
+ return
+ endtry
+
+ if l:window_type ==# 'horizontal'
+ lopen
+ elseif l:window_type ==# 'vertical'
+ vertical lopen
+ let &winwidth=(&columns/2)
+ elseif l:window_type ==# 'tab'
+ tab lopen
+ else
+ lopen
+ endif
+ setlocal modifiable
+ for i in range(1, line('$'))
+ " this is the location-list data for the current item
+ let d = getloclist(0)[i-1]
+ " atx headers
+ if match(d.text, "^#") > -1
+ let l:level = len(matchstr(d.text, '#*', 'g'))-1
+ let d.text = substitute(d.text, '\v^#*[ ]*', '', '')
+ let d.text = substitute(d.text, '\v[ ]*#*$', '', '')
+ " setex headers
+ else
+ let l:next_line = getbufline(bufname(d.bufnr), d.lnum+1)
+ if match(l:next_line, "=") > -1
+ let l:level = 0
+ elseif match(l:next_line, "-") > -1
+ let l:level = 1
+ endif
+ endif
+ call setline(i, repeat(' ', l:level). d.text)
+ endfor
+ setlocal nomodified
+ setlocal nomodifiable
+ normal! gg
+endfunction
+
+" Convert Setex headers in range `line1 .. line2` to Atx.
+"
+" Return the number of conversions.
+"
+function! s:SetexToAtx(line1, line2)
+ let l:originalNumLines = line('$')
+ execute 'silent! ' . a:line1 . ',' . a:line2 . 'substitute/\v(.*\S.*)\n\=+$/# \1/'
+ execute 'silent! ' . a:line1 . ',' . a:line2 . 'substitute/\v(.*\S.*)\n-+$/## \1/'
+ return l:originalNumLines - line('$')
+endfunction