rhelp.vim 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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: Tue Apr 07, 2015 04:38PM
  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. " Only define the functions once.
  20. if exists("*GetRHelpIndent")
  21. finish
  22. endif
  23. function s:SanitizeRHelpLine(line)
  24. let newline = substitute(a:line, '\\\\', "x", "g")
  25. let newline = substitute(newline, '\\{', "x", "g")
  26. let newline = substitute(newline, '\\}', "x", "g")
  27. let newline = substitute(newline, '\\%', "x", "g")
  28. let newline = substitute(newline, '%.*', "", "")
  29. let newline = substitute(newline, '\s*$', "", "")
  30. return newline
  31. endfunction
  32. function GetRHelpIndent()
  33. let clnum = line(".") " current line
  34. if clnum == 1
  35. return 0
  36. endif
  37. let cline = getline(clnum)
  38. if cline =~ '^\s*}\s*$'
  39. let i = clnum
  40. let bb = -1
  41. while bb != 0 && i > 1
  42. let i -= 1
  43. let line = s:SanitizeRHelpLine(getline(i))
  44. let line2 = substitute(line, "{", "", "g")
  45. let openb = strlen(line) - strlen(line2)
  46. let line3 = substitute(line2, "}", "", "g")
  47. let closeb = strlen(line2) - strlen(line3)
  48. let bb += openb - closeb
  49. endwhile
  50. return indent(i)
  51. endif
  52. if cline =~ '^\s*#ifdef\>' || cline =~ '^\s*#endif\>'
  53. return 0
  54. endif
  55. let lnum = clnum - 1
  56. let line = getline(lnum)
  57. if line =~ '^\s*#ifdef\>' || line =~ '^\s*#endif\>'
  58. let lnum -= 1
  59. let line = getline(lnum)
  60. endif
  61. while lnum > 1 && (line =~ '^\s*$' || line =~ '^#ifdef' || line =~ '^#endif')
  62. let lnum -= 1
  63. let line = getline(lnum)
  64. endwhile
  65. if lnum == 1
  66. return 0
  67. endif
  68. let line = s:SanitizeRHelpLine(line)
  69. let line2 = substitute(line, "{", "", "g")
  70. let openb = strlen(line) - strlen(line2)
  71. let line3 = substitute(line2, "}", "", "g")
  72. let closeb = strlen(line2) - strlen(line3)
  73. let bb = openb - closeb
  74. let ind = indent(lnum) + (bb * shiftwidth())
  75. if line =~ '^\s*}\s*$'
  76. let ind = indent(lnum)
  77. endif
  78. if ind < 0
  79. return 0
  80. endif
  81. return ind
  82. endfunction
  83. function GetCorrectRHelpIndent()
  84. let lastsection = search('^\\[a-z]*{', "bncW")
  85. let secname = getline(lastsection)
  86. if secname =~ '^\\usage{' || secname =~ '^\\examples{' || secname =~ '^\\dontshow{' || secname =~ '^\\dontrun{' || secname =~ '^\\donttest{' || secname =~ '^\\testonly{' || secname =~ '^\\method{.*}{.*}('
  87. return s:RIndent()
  88. else
  89. return GetRHelpIndent()
  90. endif
  91. endfunction
  92. " vim: sw=2