markdown.vim 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. if exists("b:did_indent") | finish | endif
  2. let b:did_indent = 1
  3. setlocal indentexpr=GetMarkdownIndent()
  4. setlocal nolisp
  5. setlocal autoindent
  6. " Automatically continue blockquote on line break
  7. setlocal formatoptions+=r
  8. setlocal comments=b:>
  9. if get(g:, "vim_markdown_auto_insert_bullets", 1)
  10. " Do not automatically insert bullets when auto-wrapping with text-width
  11. setlocal formatoptions-=c
  12. " Accept various markers as bullets
  13. setlocal comments+=b:*,b:+,b:-
  14. endif
  15. " Only define the function once
  16. if exists("*GetMarkdownIndent") | finish | endif
  17. function! s:IsMkdCode(lnum)
  18. let name = synIDattr(synID(a:lnum, 1, 0), 'name')
  19. return (name =~ '^mkd\%(Code$\|Snippet\)' || name != '' && name !~ '^\%(mkd\|html\)')
  20. endfunction
  21. function! s:IsLiStart(line)
  22. return a:line !~ '^ *\([*-]\)\%( *\1\)\{2}\%( \|\1\)*$' &&
  23. \ a:line =~ '^\s*[*+-] \+'
  24. endfunction
  25. function! s:IsHeaderLine(line)
  26. return a:line =~ '^\s*#'
  27. endfunction
  28. function! s:IsBlankLine(line)
  29. return a:line =~ '^$'
  30. endfunction
  31. function! s:PrevNonBlank(lnum)
  32. let i = a:lnum
  33. while i > 1 && s:IsBlankLine(getline(i))
  34. let i -= 1
  35. endwhile
  36. return i
  37. endfunction
  38. function GetMarkdownIndent()
  39. if v:lnum > 2 && s:IsBlankLine(getline(v:lnum - 1)) && s:IsBlankLine(getline(v:lnum - 2))
  40. return 0
  41. endif
  42. let list_ind = get(g:, "vim_markdown_new_list_item_indent", 4)
  43. " Find a non-blank line above the current line.
  44. let lnum = s:PrevNonBlank(v:lnum - 1)
  45. " At the start of the file use zero indent.
  46. if lnum == 0 | return 0 | endif
  47. let ind = indent(lnum)
  48. let line = getline(lnum) " Last line
  49. let cline = getline(v:lnum) " Current line
  50. if s:IsLiStart(cline)
  51. " Current line is the first line of a list item, do not change indent
  52. return indent(v:lnum)
  53. elseif s:IsHeaderLine(cline) && !s:IsMkdCode(v:lnum)
  54. " Current line is the header, do not indent
  55. return 0
  56. elseif s:IsLiStart(line)
  57. if s:IsMkdCode(lnum)
  58. return ind
  59. else
  60. " Last line is the first line of a list item, increase indent
  61. return ind + list_ind
  62. end
  63. else
  64. return ind
  65. endif
  66. endfunction