rhelp.vim 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. " Vim indent file
  2. " Language: R Documentation (Help), *.Rd
  3. " Author: Jakson Alves de Aquino <jalvesaq@gmail.com>
  4. " Homepage: https://github.com/jalvesaq/R-Vim-runtime
  5. " Last Change: Feb 25, 2023
  6. " Only load this indent file when no other was loaded.
  7. if exists("b:did_indent")
  8. finish
  9. endif
  10. runtime indent/r.vim
  11. let s:RIndent = function(substitute(&indentexpr, "()", "", ""))
  12. let b:did_indent = 1
  13. setlocal noautoindent
  14. setlocal nocindent
  15. setlocal nosmartindent
  16. setlocal nolisp
  17. setlocal indentkeys=0{,0},:,!^F,o,O,e
  18. setlocal indentexpr=GetCorrectRHelpIndent()
  19. let b:undo_indent = "setl ai< cin< inde< indk< lisp< si<"
  20. " Only define the functions once.
  21. if exists("*GetRHelpIndent")
  22. finish
  23. endif
  24. function s:SanitizeRHelpLine(line)
  25. let newline = substitute(a:line, '\\\\', "x", "g")
  26. let newline = substitute(newline, '\\{', "x", "g")
  27. let newline = substitute(newline, '\\}', "x", "g")
  28. let newline = substitute(newline, '\\%', "x", "g")
  29. let newline = substitute(newline, '%.*', "", "")
  30. let newline = substitute(newline, '\s*$', "", "")
  31. return newline
  32. endfunction
  33. function GetRHelpIndent()
  34. let clnum = line(".") " current line
  35. if clnum == 1
  36. return 0
  37. endif
  38. let cline = getline(clnum)
  39. if cline =~ '^\s*}\s*$'
  40. let i = clnum
  41. let bb = -1
  42. while bb != 0 && i > 1
  43. let i -= 1
  44. let line = s:SanitizeRHelpLine(getline(i))
  45. let line2 = substitute(line, "{", "", "g")
  46. let openb = strlen(line) - strlen(line2)
  47. let line3 = substitute(line2, "}", "", "g")
  48. let closeb = strlen(line2) - strlen(line3)
  49. let bb += openb - closeb
  50. endwhile
  51. return indent(i)
  52. endif
  53. if cline =~ '^\s*#ifdef\>' || cline =~ '^\s*#endif\>'
  54. return 0
  55. endif
  56. let lnum = clnum - 1
  57. let line = getline(lnum)
  58. if line =~ '^\s*#ifdef\>' || line =~ '^\s*#endif\>'
  59. let lnum -= 1
  60. let line = getline(lnum)
  61. endif
  62. while lnum > 1 && (line =~ '^\s*$' || line =~ '^#ifdef' || line =~ '^#endif')
  63. let lnum -= 1
  64. let line = getline(lnum)
  65. endwhile
  66. if lnum == 1
  67. return 0
  68. endif
  69. let line = s:SanitizeRHelpLine(line)
  70. let line2 = substitute(line, "{", "", "g")
  71. let openb = strlen(line) - strlen(line2)
  72. let line3 = substitute(line2, "}", "", "g")
  73. let closeb = strlen(line2) - strlen(line3)
  74. let bb = openb - closeb
  75. let ind = indent(lnum) + (bb * shiftwidth())
  76. if line =~ '^\s*}\s*$'
  77. let ind = indent(lnum)
  78. endif
  79. if ind < 0
  80. return 0
  81. endif
  82. return ind
  83. endfunction
  84. function GetCorrectRHelpIndent()
  85. let lastsection = search('^\\[a-z]*{', "bncW")
  86. let secname = getline(lastsection)
  87. if secname =~ '^\\usage{' || secname =~ '^\\examples{' || secname =~ '^\\dontshow{' || secname =~ '^\\dontrun{' || secname =~ '^\\donttest{' || secname =~ '^\\testonly{' || secname =~ '^\\method{.*}{.*}('
  88. return s:RIndent()
  89. else
  90. return GetRHelpIndent()
  91. endif
  92. endfunction
  93. " vim: sw=2