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