Clean up folding code

This commit is contained in:
Tim Pope
2019-12-01 04:25:59 -05:00
parent 06a62df8a5
commit 1a436852ac

View File

@@ -19,15 +19,17 @@ else
let b:undo_ftplugin = "setl cms< com< fo< flp<"
endif
function! MarkdownFold()
function! s:NotCodeBlock(lnum) abort
return synIDattr(synID(v:lnum, 1, 1), 'name') !=# 'markdownCode'
endfunction
function! MarkdownFold() abort
let line = getline(v:lnum)
" Regular headers
if line =~# '^#\+ ' && s:NotCodeBlock(v:lnum)
return ">" . match(line, ' ')
endif
" Setext style headings
let nextline = getline(v:lnum + 1)
if (line =~ '^.\+$') && (nextline =~ '^=\+$') && s:NotCodeBlock(v:lnum + 1)
return ">1"
@@ -40,11 +42,21 @@ function! MarkdownFold()
return "="
endfunction
function! s:NotCodeBlock(lnum)
return synIDattr(synID(v:lnum, 1, 1), 'name') !=# 'markdownCode'
function! s:HashIndent(lnum) abort
let hash_header = matchstr(getline(a:lnum), '^#\{1,6}')
if len(hash_header)
return hash_header
else
let nextline = getline(a:lnum + 1)
if nextline =~# '^=\+\s*$'
return '#'
elseif nextline =~# '^-\+\s*$'
return '##'
endif
endif
endfunction
function! MarkdownFoldText()
function! MarkdownFoldText() abort
let hash_indent = s:HashIndent(v:foldstart)
let title = substitute(getline(v:foldstart), '^#\+\s*', '', '')
let foldsize = (v:foldend - v:foldstart + 1)
@@ -52,22 +64,6 @@ function! MarkdownFoldText()
return hash_indent.' '.title.' '.linecount
endfunction
function! s:HashIndent(lnum)
let hash_header = matchstr(getline(a:lnum), '^#\{1,6}')
if len(hash_header) > 0
" hashtag header
return hash_header
else
" == or -- header
let nextline = getline(a:lnum + 1)
if nextline =~ '^=\+\s*$'
return repeat('#', 1)
elseif nextline =~ '^-\+\s*$'
return repeat('#', 2)
endif
endif
endfunction
if has("folding") && exists("g:markdown_folding")
setlocal foldexpr=MarkdownFold()
setlocal foldmethod=expr