1de6d9ecd350

fuckoing markdown

i had a nice commit message here but vim ate it.  fuck it.
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Mon, 16 Jun 2014 08:00:21 -0400
parents 4e6f3d36c241
children 8a78e472f3ea
branches/tags (none)
files vim/after/ftplugin/markdown.vim

Changes

--- a/vim/after/ftplugin/markdown.vim	Mon Jun 02 11:39:16 2014 -0700
+++ b/vim/after/ftplugin/markdown.vim	Mon Jun 16 08:00:21 2014 -0400
@@ -4,25 +4,51 @@
 " this code can be placed in file
 "   $HOME/.vim/after/ftplugin/markdown.vim
 
-func! Foldexpr_markdown(lnum)
+function! Get_Markdown_Header_Level(lnum) " {{{
     let l1 = getline(a:lnum)
 
     if l1 =~ '^\s*$'
-        " ignore empty lines
-        return '='
+        " a blank line is never a header
+        return 0
     endif
 
     let l2 = getline(a:lnum+1)
 
     if  l2 =~ '^==\+\s*'
         " next line is underlined (level 1)
-        return '>1'
+        return 1
     elseif l2 =~ '^--\+\s*'
         " next line is underlined (level 2)
-        return '>2'
+        return 2
     elseif l1 =~ '^#'
         " current line starts with hashes
-        return '>'.matchend(l1, '^#\+')
+        return matchend(l1, '^#\+')
+    endif
+
+    return 0
+endfunction " }}}
+
+function! Foldexpr_markdown(lnum) " {{{
+    let line = getline(a:lnum)
+    let anchor_re = '\v^\<a name.*'
+
+    if line =~ anchor_re
+        let next_line_hval = Get_Markdown_Header_Level(a:lnum + 1)
+        if next_line_hval
+            return '>' . next_line_hval
+        else
+            return '='
+        endif
+    endif
+
+    let line_hval = Get_Markdown_Header_Level(a:lnum)
+
+    if line_hval
+        if getline(a:lnum - 1) =~ anchor_re
+            return line_hval
+        else
+            return '>' . line_hval
+        endif
     elseif a:lnum == 1
         " fold any 'preamble'
         return '>1'
@@ -30,8 +56,30 @@
         " keep previous foldlevel
         return '='
     endif
-endfunc
+endfunction " }}}
+
+function! MarkdownFoldText() " {{{
+    let line = getline(v:foldstart)
+
+    let anchor_re = '\v^\<a name.*'
+    if line =~ anchor_re
+        let line = getline(v:foldstart + 1)
+    end
 
+    let nucolwidth = &fdc + &number * &numberwidth
+    let windowwidth = winwidth(0) - nucolwidth - 3
+    let foldedlinecount = v:foldend - v:foldstart
+
+    " expand tabs into spaces
+    let onetab = strpart('          ', 0, &tabstop)
+    let line = substitute(line, '\t', onetab, 'g')
+
+    let line = strpart(line, 0, windowwidth - 2 -len(foldedlinecount))
+    let fillcharcount = windowwidth - len(line) - len(foldedlinecount)
+    return line . '…' . repeat(" ",fillcharcount) . foldedlinecount . '…' . ' '
+endfunction " }}}
+
+setlocal foldtext=MarkdownFoldText()
 setlocal foldexpr=Foldexpr_markdown(v:lnum)
 setlocal foldmethod=expr