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