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.

52 lines
1.3 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! s:is_mkdCode(lnum)
  9. return synIDattr(synID(a:lnum, 1, 0), 'name') == 'mkdCode'
  10. endfunc
  11. func! s:effective_line(lnum)
  12. let line = getline(a:lnum)
  13. return (line !~ '^[=-#]' || s:is_mkdCode(a:lnum)) ? '' : line
  14. endfunc
  15. func! Foldexpr_markdown(lnum)
  16. let l2 = s:effective_line(a:lnum+1)
  17. if l2 =~ '^==\+\s*'
  18. " next line is underlined (level 1)
  19. return '>1'
  20. elseif l2 =~ '^--\+\s*'
  21. " next line is underlined (level 2)
  22. return '>2'
  23. endif
  24. let l1 = s:effective_line(a:lnum)
  25. if l1 =~ '^#'
  26. " don't include the section title in the fold
  27. return '-1'
  28. endif
  29. if (a:lnum == 1)
  30. let l0 = ''
  31. else
  32. let l0 = s:effective_line(a:lnum-1)
  33. endif
  34. if l0 =~ '^#'
  35. " current line starts with hashes
  36. return '>'.matchend(l0, '^#\+')
  37. else
  38. " keep previous foldlevel
  39. return '='
  40. endif
  41. endfunc
  42. if !get(g:, "vim_markdown_folding_disabled", 0)
  43. setlocal foldexpr=Foldexpr_markdown(v:lnum)
  44. setlocal foldmethod=expr
  45. endif