markdown.vim 2.9 KB

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