From 1e6798717ab5632fc277206c6bb0b40a0e252dcf Mon Sep 17 00:00:00 2001 From: Ian Edington Date: Fri, 9 Dec 2016 14:07:02 -0500 Subject: [PATCH] Added markdown foldtext function Addresses an issue metioned in tpope/vim-markdown#10 of adding a foldtext function. - Displays == and -- headers as # and ## - Use markdown header style instead of '+--' to show nesting Given the example of This is Header One ================== This is Header Two ----------------- ### This is Header Three This is a normal line Instead of: +-- 6 lines: This is Header One----------------------------- +--- 4 lines: This is Header Two---------------------------- +---- 2 lines: ### This is Header Three--------------------- You get: # This is Header One [6 lines]------------------------------- ## This is Header Two [4 lines]------------------------------ ### This is Header Three [2 lines]---------------------------- --- ftplugin/markdown.vim | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/ftplugin/markdown.vim b/ftplugin/markdown.vim index f406db5..2fbdf66 100644 --- a/ftplugin/markdown.vim +++ b/ftplugin/markdown.vim @@ -41,10 +41,35 @@ function! MarkdownFold() return "=" endfunction +function! MarkdownFoldText() + let hash_indent = s:HashIndent(v:foldstart) + let title = substitute(getline(v:foldstart), '^#\+\s*', '', '') + let foldsize = (v:foldend - v:foldstart + 1) + let linecount = '['.foldsize.' lines]' + 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 - let b:undo_ftplugin .= " foldexpr< foldmethod<" + setlocal foldtext=MarkdownFoldText() + let b:undo_ftplugin .= " foldexpr< foldmethod< foldtext<" endif " vim:set sw=2: