markdown.vim 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. " Vim filetype plugin
  2. " Language: Markdown
  3. " Maintainer: Tim Pope <vimNOSPAM@tpope.org>
  4. " Last Change: 2019 Dec 05
  5. if exists("b:did_ftplugin")
  6. finish
  7. endif
  8. runtime! ftplugin/html.vim ftplugin/html_*.vim ftplugin/html/*.vim
  9. setlocal comments=fb:*,fb:-,fb:+,n:> commentstring=<!--%s-->
  10. setlocal formatoptions+=tcqln formatoptions-=r formatoptions-=o
  11. setlocal formatlistpat=^\\s*\\d\\+\\.\\s\\+\\\|^[-*+]\\s\\+\\\|^\\[^\\ze[^\\]]\\+\\]:
  12. if exists('b:undo_ftplugin')
  13. let b:undo_ftplugin .= "|setl cms< com< fo< flp<"
  14. else
  15. let b:undo_ftplugin = "setl cms< com< fo< flp<"
  16. endif
  17. function! s:NotCodeBlock(lnum) abort
  18. return synIDattr(synID(v:lnum, 1, 1), 'name') !=# 'markdownCode'
  19. endfunction
  20. function! MarkdownFold() abort
  21. let line = getline(v:lnum)
  22. if line =~# '^#\+ ' && s:NotCodeBlock(v:lnum)
  23. return ">" . match(line, ' ')
  24. endif
  25. let nextline = getline(v:lnum + 1)
  26. if (line =~ '^.\+$') && (nextline =~ '^=\+$') && s:NotCodeBlock(v:lnum + 1)
  27. return ">1"
  28. endif
  29. if (line =~ '^.\+$') && (nextline =~ '^-\+$') && s:NotCodeBlock(v:lnum + 1)
  30. return ">2"
  31. endif
  32. return "="
  33. endfunction
  34. function! s:HashIndent(lnum) abort
  35. let hash_header = matchstr(getline(a:lnum), '^#\{1,6}')
  36. if len(hash_header)
  37. return hash_header
  38. else
  39. let nextline = getline(a:lnum + 1)
  40. if nextline =~# '^=\+\s*$'
  41. return '#'
  42. elseif nextline =~# '^-\+\s*$'
  43. return '##'
  44. endif
  45. endif
  46. endfunction
  47. function! MarkdownFoldText() abort
  48. let hash_indent = s:HashIndent(v:foldstart)
  49. let title = substitute(getline(v:foldstart), '^#\+\s*', '', '')
  50. let foldsize = (v:foldend - v:foldstart + 1)
  51. let linecount = '['.foldsize.' lines]'
  52. return hash_indent.' '.title.' '.linecount
  53. endfunction
  54. if has("folding") && exists("g:markdown_folding")
  55. setlocal foldexpr=MarkdownFold()
  56. setlocal foldmethod=expr
  57. setlocal foldtext=MarkdownFoldText()
  58. let b:undo_ftplugin .= " foldexpr< foldmethod< foldtext<"
  59. endif
  60. " vim:set sw=2: