rst.vim 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. " 2023 Aug 28 by Vim Project (undo_indent)
  8. if exists("b:did_indent")
  9. finish
  10. endif
  11. let b:did_indent = 1
  12. setlocal indentexpr=GetRSTIndent()
  13. setlocal indentkeys=!^F,o,O
  14. setlocal nosmartindent
  15. let b:undo_indent = "setlocal indentexpr< indentkeys< smartindent<"
  16. if exists("*GetRSTIndent")
  17. finish
  18. endif
  19. let s:itemization_pattern = '^\s*[-*+]\s'
  20. let s:enumeration_pattern = '^\s*\%(\d\+\|#\)\.\s\+'
  21. let s:note_pattern = '^\.\. '
  22. function! s:get_paragraph_start()
  23. let paragraph_mark_start = getpos("'{")[1]
  24. return getline(paragraph_mark_start) =~ '\S' ? paragraph_mark_start : paragraph_mark_start + 1
  25. endfunction
  26. function GetRSTIndent()
  27. let lnum = prevnonblank(v:lnum - 1)
  28. if lnum == 0
  29. return 0
  30. endif
  31. let ind = indent(lnum)
  32. let line = getline(lnum)
  33. let psnum = s:get_paragraph_start()
  34. if psnum != 0
  35. if getline(psnum) =~ s:note_pattern
  36. let ind = 3
  37. endif
  38. endif
  39. if line =~ s:itemization_pattern
  40. let ind += 2
  41. elseif line =~ s:enumeration_pattern
  42. let ind += matchend(line, s:enumeration_pattern)
  43. endif
  44. let line = getline(v:lnum - 1)
  45. " Indent :FIELD: lines. Don’t match if there is no text after the field or
  46. " if the text ends with a sent-ender.
  47. if line =~ '^:.\+:\s\{-1,\}\S.\+[^.!?:]$'
  48. return matchend(line, '^:.\{-1,}:\s\+')
  49. endif
  50. if line =~ '^\s*$'
  51. execute lnum
  52. call search('^\s*\%([-*+]\s\|\%(\d\+\|#\)\.\s\|\.\.\|$\)', 'bW')
  53. let line = getline('.')
  54. if line =~ s:itemization_pattern
  55. let ind -= 2
  56. elseif line =~ s:enumeration_pattern
  57. let ind -= matchend(line, s:enumeration_pattern)
  58. elseif line =~ '^\s*\.\.'
  59. let ind -= 3
  60. endif
  61. endif
  62. return ind
  63. endfunction