markdown.vim 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. " vim: ts=4 sw=4:
  2. " folding for Markdown headers, both styles (atx- and setex-)
  3. " http://daringfireball.net/projects/markdown/syntax#header
  4. "
  5. " this code can be placed in file
  6. " $HOME/.vim/after/ftplugin/markdown.vim
  7. "
  8. " original version from Steve Losh's gist: https://gist.github.com/1038710
  9. function! s:is_mkdCode(lnum)
  10. let name = synIDattr(synID(a:lnum, 1, 0), 'name')
  11. return (name =~# '^mkd\%(Code$\|Snippet\)' || name !=# '' && name !~? '^\%(mkd\|html\)')
  12. endfunction
  13. if get(g:, 'vim_markdown_folding_style_pythonic', 0)
  14. function! Foldexpr_markdown(lnum)
  15. let l1 = getline(a:lnum)
  16. "~~~~~ keep track of fenced code blocks ~~~~~
  17. "If we hit a code block fence
  18. if l1 =~# '````*' || l1 =~# '\~\~\~\~*'
  19. " toggle the variable that says if we're in a code block
  20. if b:fenced_block == 0
  21. let b:fenced_block = 1
  22. elseif b:fenced_block == 1
  23. let b:fenced_block = 0
  24. endif
  25. " else, if we're caring about front matter
  26. elseif g:vim_markdown_frontmatter == 1
  27. " if we're in front matter and not on line 1
  28. if b:front_matter == 1 && a:lnum > 2
  29. let l0 = getline(a:lnum-1)
  30. " if the previous line fenced front matter
  31. if l0 ==# '---'
  32. " we must not be in front matter
  33. let b:front_matter = 0
  34. endif
  35. " else, if we're on line one
  36. elseif a:lnum == 1
  37. " if we hit a front matter fence
  38. if l1 ==# '---'
  39. " we're in the front matter
  40. let b:front_matter = 1
  41. endif
  42. endif
  43. endif
  44. " if we're in a code block or front matter
  45. if b:fenced_block ==# 1 || b:front_matter ==# 1
  46. if a:lnum ==# 1
  47. " fold any 'preamble'
  48. return '>1'
  49. else
  50. " keep previous foldlevel
  51. return '='
  52. endif
  53. endif
  54. let l2 = getline(a:lnum+1)
  55. " if the next line starts with two or more '='
  56. " and is not code
  57. if l2 =~# '^==\+\s*' && !s:is_mkdCode(a:lnum+1)
  58. " next line is underlined (level 1)
  59. return '>0'
  60. " else, if the nex line starts with two or more '-'
  61. " but is not comment closer (-->)
  62. " and is not code
  63. elseif l2 =~# '^--\+\s*$' && !s:is_mkdCode(a:lnum+1)
  64. " next line is underlined (level 2)
  65. return '>1'
  66. endif
  67. "if we're on a non-code line starting with a pound sign
  68. if l1 =~# '^#' && !s:is_mkdCode(a:lnum)
  69. " set the fold level to the number of hashes -1
  70. " return '>'.(matchend(l1, '^#\+') - 1)
  71. " set the fold level to the number of hashes
  72. return '>'.(matchend(l1, '^#\+'))
  73. " else, if we're on line 1
  74. elseif a:lnum == 1
  75. " fold any 'preamble'
  76. return '>1'
  77. else
  78. " keep previous foldlevel
  79. return '='
  80. endif
  81. endfunction
  82. function! Foldtext_markdown()
  83. let line = getline(v:foldstart)
  84. let has_numbers = &number || &relativenumber
  85. let nucolwidth = &foldcolumn + has_numbers * &numberwidth
  86. let windowwidth = winwidth(0) - nucolwidth - 6
  87. let foldedlinecount = v:foldend - v:foldstart
  88. let line = strpart(line, 0, windowwidth - 2 -len(foldedlinecount))
  89. let line = substitute(line, '\%("""\|''''''\)', '', '')
  90. let fillcharcount = windowwidth - len(line) - len(foldedlinecount) + 1
  91. return line . ' ' . repeat('-', fillcharcount) . ' ' . foldedlinecount
  92. endfunction
  93. else " vim_markdown_folding_style_pythonic == 0
  94. function! Foldexpr_markdown(lnum)
  95. if (a:lnum == 1)
  96. let l0 = ''
  97. else
  98. let l0 = getline(a:lnum-1)
  99. endif
  100. " keep track of fenced code blocks
  101. if l0 =~# '````*' || l0 =~# '\~\~\~\~*'
  102. if b:fenced_block == 0
  103. let b:fenced_block = 1
  104. elseif b:fenced_block == 1
  105. let b:fenced_block = 0
  106. endif
  107. elseif g:vim_markdown_frontmatter == 1
  108. if b:front_matter == 1
  109. if l0 ==# '---'
  110. let b:front_matter = 0
  111. endif
  112. elseif a:lnum == 2
  113. if l0 ==# '---'
  114. let b:front_matter = 1
  115. endif
  116. endif
  117. endif
  118. if b:fenced_block == 1 || b:front_matter == 1
  119. " keep previous foldlevel
  120. return '='
  121. endif
  122. let l2 = getline(a:lnum+1)
  123. if l2 =~# '^==\+\s*' && !s:is_mkdCode(a:lnum+1)
  124. " next line is underlined (level 1)
  125. return '>1'
  126. elseif l2 =~# '^--\+\s*$' && !s:is_mkdCode(a:lnum+1)
  127. " next line is underlined (level 2)
  128. if s:vim_markdown_folding_level >= 2
  129. return '>1'
  130. else
  131. return '>2'
  132. endif
  133. endif
  134. let l1 = getline(a:lnum)
  135. if l1 =~# '^#' && !s:is_mkdCode(a:lnum)
  136. " fold level according to option
  137. if s:vim_markdown_folding_level == 1 || matchend(l1, '^#\+') > s:vim_markdown_folding_level
  138. if a:lnum == line('$')
  139. return matchend(l1, '^#\+') - 1
  140. else
  141. return -1
  142. endif
  143. else
  144. " headers are not folded
  145. return 0
  146. endif
  147. endif
  148. if l0 =~# '^#' && !s:is_mkdCode(a:lnum-1)
  149. " previous line starts with hashes
  150. return '>'.matchend(l0, '^#\+')
  151. else
  152. " keep previous foldlevel
  153. return '='
  154. endif
  155. endfunction
  156. endif
  157. let b:fenced_block = 0
  158. let b:front_matter = 0
  159. let s:vim_markdown_folding_level = get(g:, 'vim_markdown_folding_level', 1)
  160. function! s:MarkdownSetupFolding()
  161. if !get(g:, 'vim_markdown_folding_disabled', 0)
  162. if get(g:, 'vim_markdown_folding_style_pythonic', 0)
  163. if get(g:, 'vim_markdown_override_foldtext', 1)
  164. setlocal foldtext=Foldtext_markdown()
  165. endif
  166. endif
  167. setlocal foldexpr=Foldexpr_markdown(v:lnum)
  168. setlocal foldmethod=expr
  169. endif
  170. endfunction
  171. function! s:MarkdownSetupFoldLevel()
  172. if get(g:, 'vim_markdown_folding_style_pythonic', 0)
  173. " set default foldlevel
  174. execute 'setlocal foldlevel='.s:vim_markdown_folding_level
  175. endif
  176. endfunction
  177. call s:MarkdownSetupFoldLevel()
  178. call s:MarkdownSetupFolding()
  179. augroup Mkd
  180. " These autocmds need to be kept in sync with the autocmds calling
  181. " s:MarkdownRefreshSyntax in ftplugin/markdown.vim.
  182. autocmd BufWinEnter,BufWritePost <buffer> call s:MarkdownSetupFolding()
  183. autocmd InsertEnter,InsertLeave <buffer> call s:MarkdownSetupFolding()
  184. autocmd CursorHold,CursorHoldI <buffer> call s:MarkdownSetupFolding()
  185. augroup END