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