From 1a436852acde2fc52b8d7c90ff002f8be34a9d92 Mon Sep 17 00:00:00 2001 From: Tim Pope Date: Sun, 1 Dec 2019 04:25:59 -0500 Subject: [PATCH] Clean up folding code --- ftplugin/markdown.vim | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/ftplugin/markdown.vim b/ftplugin/markdown.vim index 7cda6b1..79ec151 100644 --- a/ftplugin/markdown.vim +++ b/ftplugin/markdown.vim @@ -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