rhelp.vim 3.0 KB

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