rst.vim 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. " Vim indent file
  2. " Vim reST indent file
  3. " Language: reStructuredText Documentation Format
  4. " Maintainer: Marshall Ward <marshall.ward@gmail.com>
  5. " Previous Maintainer: Nikolai Weibull <now@bitwi.se>
  6. " Latest Revision: 2020-03-31
  7. if exists("b:did_indent")
  8. finish
  9. endif
  10. let b:did_indent = 1
  11. setlocal indentexpr=GetRSTIndent()
  12. setlocal indentkeys=!^F,o,O
  13. setlocal nosmartindent
  14. if exists("*GetRSTIndent")
  15. finish
  16. endif
  17. let s:itemization_pattern = '^\s*[-*+]\s'
  18. let s:enumeration_pattern = '^\s*\%(\d\+\|#\)\.\s\+'
  19. let s:note_pattern = '^\.\. '
  20. function! s:get_paragraph_start()
  21. let paragraph_mark_start = getpos("'{")[1]
  22. return getline(paragraph_mark_start) =~ '\S' ? paragraph_mark_start : paragraph_mark_start + 1
  23. endfunction
  24. function GetRSTIndent()
  25. let lnum = prevnonblank(v:lnum - 1)
  26. if lnum == 0
  27. return 0
  28. endif
  29. let ind = indent(lnum)
  30. let line = getline(lnum)
  31. let psnum = s:get_paragraph_start()
  32. if psnum != 0
  33. if getline(psnum) =~ s:note_pattern
  34. let ind = 3
  35. endif
  36. endif
  37. if line =~ s:itemization_pattern
  38. let ind += 2
  39. elseif line =~ s:enumeration_pattern
  40. let ind += matchend(line, s:enumeration_pattern)
  41. endif
  42. let line = getline(v:lnum - 1)
  43. " Indent :FIELD: lines. Don’t match if there is no text after the field or
  44. " if the text ends with a sent-ender.
  45. if line =~ '^:.\+:\s\{-1,\}\S.\+[^.!?:]$'
  46. return matchend(line, '^:.\{-1,}:\s\+')
  47. endif
  48. if line =~ '^\s*$'
  49. execute lnum
  50. call search('^\s*\%([-*+]\s\|\%(\d\+\|#\)\.\s\|\.\.\|$\)', 'bW')
  51. let line = getline('.')
  52. if line =~ s:itemization_pattern
  53. let ind -= 2
  54. elseif line =~ s:enumeration_pattern
  55. let ind -= matchend(line, s:enumeration_pattern)
  56. elseif line =~ '^\s*\.\.'
  57. let ind -= 3
  58. endif
  59. endif
  60. return ind
  61. endfunction