Markdown Vim Mode
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.5 KiB

  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 insert bullets
  7. setlocal formatoptions+=r
  8. " Do not automatically insert bullets when auto-wrapping with text-width
  9. setlocal formatoptions-=c
  10. " Accept various markers as bullets
  11. setlocal comments=b:*,b:+,b:-
  12. " Automatically continue blockquote on line break
  13. setlocal comments+=b:>
  14. " Only define the function once
  15. if exists("*GetMarkdownIndent") | finish | endif
  16. function! s:is_li_start(line)
  17. return a:line !~ '^ *\([*-]\)\%( *\1\)\{2}\%( \|\1\)*$' &&
  18. \ a:line =~ '^\s*[*+-] \+'
  19. endfunction
  20. function! s:is_blank_line(line)
  21. return a:line =~ '^$'
  22. endfunction
  23. function! s:prevnonblank(lnum)
  24. let i = a:lnum
  25. while i > 1 && s:is_blank_line(getline(i))
  26. let i -= 1
  27. endwhile
  28. return i
  29. endfunction
  30. function GetMarkdownIndent()
  31. let list_ind = 4
  32. " Find a non-blank line above the current line.
  33. let lnum = prevnonblank(v:lnum - 1)
  34. " At the start of the file use zero indent.
  35. if lnum == 0 | return 0 | endif
  36. let ind = indent(lnum)
  37. let line = getline(lnum) " Last line
  38. let cline = getline(v:lnum) " Current line
  39. if s:is_li_start(cline)
  40. " Current line is the first line of a list item, do not change indent
  41. return indent(v:lnum)
  42. elseif s:is_li_start(line)
  43. " Last line is the first line of a list item, increase indent
  44. return ind + list_ind
  45. else
  46. return ind
  47. endif
  48. endfunction