Markdown Vim Mode
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.0 KiB

  1. " folding for Markdown headers, both styles (atx- and setex-)
  2. " http://daringfireball.net/projects/markdown/syntax#header
  3. "
  4. " this code can be placed in file
  5. " $HOME/.vim/after/ftplugin/markdown.vim
  6. "
  7. " original version from Steve Losh's gist: https://gist.github.com/1038710
  8. func! Foldexpr_markdown(lnum)
  9. if (a:lnum == 1)
  10. let l0 = ''
  11. else
  12. let l0 = getline(a:lnum-1)
  13. endif
  14. let l1 = getline(a:lnum)
  15. let l2 = getline(a:lnum+1)
  16. if l2 =~ '^==\+\s*'
  17. " next line is underlined (level 1)
  18. return '>1'
  19. elseif l2 =~ '^--\+\s*'
  20. " next line is underlined (level 2)
  21. return '>2'
  22. elseif l1 =~ '^#'
  23. " don't include the section title in the fold
  24. return '-1'
  25. elseif l0 =~ '^#'
  26. " current line starts with hashes
  27. return '>'.matchend(l0, '^#\+')
  28. else
  29. " keep previous foldlevel
  30. return '='
  31. endif
  32. endfunc
  33. if !get(g:, "vim_markdown_folding_disabled", 0)
  34. setlocal foldexpr=Foldexpr_markdown(v:lnum)
  35. setlocal foldmethod=expr
  36. endif