markdown.vim 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. " Vim filetype plugin
  2. " Language: Markdown
  3. " Maintainer: Tim Pope <https://github.com/tpope/vim-markdown>
  4. " Last Change: 2022 Oct 13
  5. if exists("b:did_ftplugin")
  6. finish
  7. endif
  8. runtime! ftplugin/html.vim ftplugin/html_*.vim ftplugin/html/*.vim
  9. let s:keepcpo= &cpo
  10. set cpo&vim
  11. setlocal comments=fb:*,fb:-,fb:+,n:> commentstring=<!--%s-->
  12. setlocal formatoptions+=tcqln formatoptions-=r formatoptions-=o
  13. setlocal formatlistpat=^\\s*\\d\\+\\.\\s\\+\\\|^\\s*[-*+]\\s\\+\\\|^\\[^\\ze[^\\]]\\+\\]:\\&^.\\{4\\}
  14. if exists('b:undo_ftplugin')
  15. let b:undo_ftplugin .= "|setl cms< com< fo< flp< et< ts< sts< sw<"
  16. else
  17. let b:undo_ftplugin = "setl cms< com< fo< flp< et< ts< sts< sw<"
  18. endif
  19. if get(g:, 'markdown_recommended_style', 1)
  20. setlocal expandtab tabstop=4 softtabstop=4 shiftwidth=4
  21. endif
  22. if !exists("g:no_plugin_maps") && !exists("g:no_markdown_maps")
  23. nnoremap <silent><buffer> [[ :<C-U>call search('\%(^#\{1,5\}\s\+\S\\|^\S.*\n^[=-]\+$\)', "bsW")<CR>
  24. nnoremap <silent><buffer> ]] :<C-U>call search('\%(^#\{1,5\}\s\+\S\\|^\S.*\n^[=-]\+$\)', "sW")<CR>
  25. xnoremap <silent><buffer> [[ :<C-U>exe "normal! gv"<Bar>call search('\%(^#\{1,5\}\s\+\S\\|^\S.*\n^[=-]\+$\)', "bsW")<CR>
  26. xnoremap <silent><buffer> ]] :<C-U>exe "normal! gv"<Bar>call search('\%(^#\{1,5\}\s\+\S\\|^\S.*\n^[=-]\+$\)', "sW")<CR>
  27. let b:undo_ftplugin .= '|sil! nunmap <buffer> [[|sil! nunmap <buffer> ]]|sil! xunmap <buffer> [[|sil! xunmap <buffer> ]]'
  28. endif
  29. function! s:NotCodeBlock(lnum) abort
  30. return synIDattr(synID(a:lnum, 1, 1), 'name') !=# 'markdownCode'
  31. endfunction
  32. function! MarkdownFold() abort
  33. let line = getline(v:lnum)
  34. if line =~# '^#\+ ' && s:NotCodeBlock(v:lnum)
  35. return ">" . match(line, ' ')
  36. endif
  37. let nextline = getline(v:lnum + 1)
  38. if (line =~ '^.\+$') && (nextline =~ '^=\+$') && s:NotCodeBlock(v:lnum + 1)
  39. return ">1"
  40. endif
  41. if (line =~ '^.\+$') && (nextline =~ '^-\+$') && s:NotCodeBlock(v:lnum + 1)
  42. return ">2"
  43. endif
  44. return "="
  45. endfunction
  46. function! s:HashIndent(lnum) abort
  47. let hash_header = matchstr(getline(a:lnum), '^#\{1,6}')
  48. if len(hash_header)
  49. return hash_header
  50. else
  51. let nextline = getline(a:lnum + 1)
  52. if nextline =~# '^=\+\s*$'
  53. return '#'
  54. elseif nextline =~# '^-\+\s*$'
  55. return '##'
  56. endif
  57. endif
  58. endfunction
  59. function! MarkdownFoldText() abort
  60. let hash_indent = s:HashIndent(v:foldstart)
  61. let title = substitute(getline(v:foldstart), '^#\+\s*', '', '')
  62. let foldsize = (v:foldend - v:foldstart + 1)
  63. let linecount = '['.foldsize.' lines]'
  64. return hash_indent.' '.title.' '.linecount
  65. endfunction
  66. if has("folding") && get(g:, "markdown_folding", 0)
  67. setlocal foldexpr=MarkdownFold()
  68. setlocal foldmethod=expr
  69. setlocal foldtext=MarkdownFoldText()
  70. let b:undo_ftplugin .= "|setl foldexpr< foldmethod< foldtext<"
  71. endif
  72. let &cpo = s:keepcpo
  73. unlet s:keepcpo
  74. " vim:set sw=2: