blob: 373b0e65dfaae90dfed2d63de7f9c6719ce21f9b [file] [log] [blame]
Bram Moolenaar5302d9e2011-09-14 17:55:08 +02001" Vim indent file
2" Language: R
3" Author: Jakson Alves de Aquino <jalvesaq@gmail.com>
Bram Moolenaar77cdfd12016-03-12 12:57:59 +01004" Homepage: https://github.com/jalvesaq/R-Vim-runtime
5" Last Change: Thu Feb 18, 2016 06:32AM
Bram Moolenaar5302d9e2011-09-14 17:55:08 +02006
7
8" Only load this indent file when no other was loaded.
Bram Moolenaar26402cb2013-02-20 21:26:00 +01009if exists("b:did_indent")
Bram Moolenaar541f92d2015-06-19 13:27:23 +020010 finish
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020011endif
Bram Moolenaar26402cb2013-02-20 21:26:00 +010012let b:did_indent = 1
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020013
14setlocal indentkeys=0{,0},:,!^F,o,O,e
15setlocal indentexpr=GetRIndent()
16
17" Only define the function once.
18if exists("*GetRIndent")
Bram Moolenaar541f92d2015-06-19 13:27:23 +020019 finish
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020020endif
21
22" Options to make the indentation more similar to Emacs/ESS:
23if !exists("g:r_indent_align_args")
Bram Moolenaar541f92d2015-06-19 13:27:23 +020024 let g:r_indent_align_args = 1
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020025endif
26if !exists("g:r_indent_ess_comments")
Bram Moolenaar541f92d2015-06-19 13:27:23 +020027 let g:r_indent_ess_comments = 0
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020028endif
29if !exists("g:r_indent_comment_column")
Bram Moolenaar541f92d2015-06-19 13:27:23 +020030 let g:r_indent_comment_column = 40
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020031endif
32if ! exists("g:r_indent_ess_compatible")
Bram Moolenaar541f92d2015-06-19 13:27:23 +020033 let g:r_indent_ess_compatible = 0
34endif
35if ! exists("g:r_indent_op_pattern")
Bram Moolenaar77cdfd12016-03-12 12:57:59 +010036 let g:r_indent_op_pattern = '\(&\||\|+\|-\|\*\|/\|=\|\~\|%\|->\)\s*$'
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020037endif
38
39function s:RDelete_quotes(line)
Bram Moolenaar541f92d2015-06-19 13:27:23 +020040 let i = 0
41 let j = 0
42 let line1 = ""
43 let llen = strlen(a:line)
44 while i < llen
45 if a:line[i] == '"'
46 let i += 1
47 let line1 = line1 . 's'
48 while !(a:line[i] == '"' && ((i > 1 && a:line[i-1] == '\' && a:line[i-2] == '\') || a:line[i-1] != '\')) && i < llen
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020049 let i += 1
Bram Moolenaar541f92d2015-06-19 13:27:23 +020050 endwhile
51 if a:line[i] == '"'
52 let i += 1
53 endif
54 else
55 if a:line[i] == "'"
56 let i += 1
57 let line1 = line1 . 's'
58 while !(a:line[i] == "'" && ((i > 1 && a:line[i-1] == '\' && a:line[i-2] == '\') || a:line[i-1] != '\')) && i < llen
59 let i += 1
60 endwhile
61 if a:line[i] == "'"
62 let i += 1
63 endif
64 else
65 if a:line[i] == "`"
66 let i += 1
67 let line1 = line1 . 's'
68 while a:line[i] != "`" && i < llen
69 let i += 1
70 endwhile
71 if a:line[i] == "`"
72 let i += 1
73 endif
74 endif
75 endif
76 endif
77 if i == llen
78 break
79 endif
80 let line1 = line1 . a:line[i]
81 let j += 1
82 let i += 1
83 endwhile
84 return line1
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020085endfunction
86
87" Convert foo(bar()) int foo()
88function s:RDelete_parens(line)
Bram Moolenaar541f92d2015-06-19 13:27:23 +020089 if s:Get_paren_balance(a:line, "(", ")") != 0
90 return a:line
91 endif
92 let i = 0
93 let j = 0
94 let line1 = ""
95 let llen = strlen(a:line)
96 while i < llen
97 let line1 = line1 . a:line[i]
98 if a:line[i] == '('
99 let nop = 1
100 while nop > 0 && i < llen
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200101 let i += 1
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200102 if a:line[i] == ')'
103 let nop -= 1
104 else
105 if a:line[i] == '('
106 let nop += 1
107 endif
108 endif
109 endwhile
110 let line1 = line1 . a:line[i]
111 endif
112 let i += 1
113 endwhile
114 return line1
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200115endfunction
116
117function! s:Get_paren_balance(line, o, c)
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200118 let line2 = substitute(a:line, a:o, "", "g")
119 let openp = strlen(a:line) - strlen(line2)
120 let line3 = substitute(line2, a:c, "", "g")
121 let closep = strlen(line2) - strlen(line3)
122 return openp - closep
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200123endfunction
124
125function! s:Get_matching_brace(linenr, o, c, delbrace)
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200126 let line = SanitizeRLine(getline(a:linenr))
127 if a:delbrace == 1
128 let line = substitute(line, '{$', "", "")
129 endif
130 let pb = s:Get_paren_balance(line, a:o, a:c)
131 let i = a:linenr
132 while pb != 0 && i > 1
133 let i -= 1
134 let pb += s:Get_paren_balance(SanitizeRLine(getline(i)), a:o, a:c)
135 endwhile
136 return i
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200137endfunction
138
139" This function is buggy because there 'if's without 'else'
140" It must be rewritten relying more on indentation
141function! s:Get_matching_if(linenr, delif)
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200142 let line = SanitizeRLine(getline(a:linenr))
143 if a:delif
144 let line = substitute(line, "if", "", "g")
145 endif
146 let elsenr = 0
147 let i = a:linenr
148 let ifhere = 0
149 while i > 0
150 let line2 = substitute(line, '\<else\>', "xxx", "g")
151 let elsenr += strlen(line) - strlen(line2)
152 if line =~ '.*\s*if\s*()' || line =~ '.*\s*if\s*()'
153 let elsenr -= 1
154 if elsenr == 0
155 let ifhere = i
156 break
157 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200158 endif
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200159 let i -= 1
160 let line = SanitizeRLine(getline(i))
161 endwhile
162 if ifhere
163 return ifhere
164 else
165 return a:linenr
166 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200167endfunction
168
169function! s:Get_last_paren_idx(line, o, c, pb)
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200170 let blc = a:pb
171 let line = substitute(a:line, '\t', s:curtabstop, "g")
172 let theidx = -1
173 let llen = strlen(line)
174 let idx = 0
175 while idx < llen
176 if line[idx] == a:o
177 let blc -= 1
178 if blc == 0
179 let theidx = idx
180 endif
181 else
182 if line[idx] == a:c
183 let blc += 1
184 endif
185 endif
186 let idx += 1
187 endwhile
188 return theidx + 1
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200189endfunction
190
191" Get previous relevant line. Search back until getting a line that isn't
192" comment or blank
193function s:Get_prev_line(lineno)
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200194 let lnum = a:lineno - 1
195 let data = getline( lnum )
196 while lnum > 0 && (data =~ '^\s*#' || data =~ '^\s*$')
197 let lnum = lnum - 1
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200198 let data = getline( lnum )
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200199 endwhile
200 return lnum
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200201endfunction
202
203" This function is also used by r-plugin/common_global.vim
204" Delete from '#' to the end of the line, unless the '#' is inside a string.
205function SanitizeRLine(line)
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200206 let newline = s:RDelete_quotes(a:line)
207 let newline = s:RDelete_parens(newline)
208 let newline = substitute(newline, '#.*', "", "")
209 let newline = substitute(newline, '\s*$', "", "")
210 if &filetype == "rhelp" && newline =~ '^\\method{.*}{.*}(.*'
211 let newline = substitute(newline, '^\\method{\(.*\)}{.*}', '\1', "")
212 endif
213 return newline
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200214endfunction
215
216function GetRIndent()
217
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200218 let clnum = line(".") " current line
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200219
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200220 let cline = getline(clnum)
221 if cline =~ '^\s*#'
222 if g:r_indent_ess_comments == 1
223 if cline =~ '^\s*###'
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200224 return 0
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200225 endif
226 if cline !~ '^\s*##'
227 return g:r_indent_comment_column
228 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200229 endif
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200230 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200231
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200232 let cline = SanitizeRLine(cline)
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200233
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200234 if cline =~ '^\s*}' || cline =~ '^\s*}\s*)$'
235 let indline = s:Get_matching_brace(clnum, '{', '}', 1)
236 if indline > 0 && indline != clnum
237 let iline = SanitizeRLine(getline(indline))
238 if s:Get_paren_balance(iline, "(", ")") == 0 || iline =~ '(\s*{$'
239 return indent(indline)
240 else
241 let indline = s:Get_matching_brace(indline, '(', ')', 1)
242 return indent(indline)
243 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200244 endif
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200245 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200246
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200247 " Find the first non blank line above the current line
248 let lnum = s:Get_prev_line(clnum)
249 " Hit the start of the file, use zero indent.
250 if lnum == 0
251 return 0
252 endif
253
254 let line = SanitizeRLine(getline(lnum))
255
256 if &filetype == "rhelp"
257 if cline =~ '^\\dontshow{' || cline =~ '^\\dontrun{' || cline =~ '^\\donttest{' || cline =~ '^\\testonly{'
258 return 0
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200259 endif
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200260 if line =~ '^\\examples{' || line =~ '^\\usage{' || line =~ '^\\dontshow{' || line =~ '^\\dontrun{' || line =~ '^\\donttest{' || line =~ '^\\testonly{'
261 return 0
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200262 endif
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200263 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200264
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200265 if &filetype == "rnoweb" && line =~ "^<<.*>>="
266 return 0
267 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200268
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100269 if cline =~ '^\s*{' && s:Get_paren_balance(cline, '{', '}') > 0
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200270 if g:r_indent_ess_compatible && line =~ ')$'
271 let nlnum = lnum
272 let nline = line
273 while s:Get_paren_balance(nline, '(', ')') < 0
274 let nlnum = s:Get_prev_line(nlnum)
275 let nline = SanitizeRLine(getline(nlnum)) . nline
276 endwhile
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200277 if nline =~ '^\s*function\s*(' && indent(nlnum) == shiftwidth()
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200278 return 0
279 endif
280 endif
281 if s:Get_paren_balance(line, "(", ")") == 0
282 return indent(lnum)
283 endif
284 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200285
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200286 " line is an incomplete command:
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100287 if line =~ '\<\(if\|while\|for\|function\)\s*()$' || line =~ '\<else$' || line =~ '<-$' || line =~ '->$'
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200288 return indent(lnum) + shiftwidth()
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200289 endif
290
291 " Deal with () and []
292
293 let pb = s:Get_paren_balance(line, '(', ')')
294
295 if line =~ '^\s*{$' || line =~ '(\s*{' || (pb == 0 && (line =~ '{$' || line =~ '(\s*{$'))
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200296 return indent(lnum) + shiftwidth()
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200297 endif
298
299 let s:curtabstop = repeat(' ', &tabstop)
300
301 if g:r_indent_align_args == 1
302 if pb > 0 && line =~ '{$'
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200303 return s:Get_last_paren_idx(line, '(', ')', pb) + shiftwidth()
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200304 endif
305
306 let bb = s:Get_paren_balance(line, '[', ']')
307
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200308 if pb > 0
309 if &filetype == "rhelp"
310 let ind = s:Get_last_paren_idx(line, '(', ')', pb)
311 else
312 let ind = s:Get_last_paren_idx(getline(lnum), '(', ')', pb)
313 endif
314 return ind
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200315 endif
316
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200317 if pb < 0 && line =~ '.*[,&|\-\*+<>]$'
318 let lnum = s:Get_prev_line(lnum)
319 while pb < 1 && lnum > 0
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200320 let line = SanitizeRLine(getline(lnum))
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200321 let line = substitute(line, '\t', s:curtabstop, "g")
322 let ind = strlen(line)
323 while ind > 0
324 if line[ind] == ')'
325 let pb -= 1
326 else
327 if line[ind] == '('
328 let pb += 1
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200329 endif
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200330 endif
331 if pb == 1
332 return ind + 1
333 endif
334 let ind -= 1
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200335 endwhile
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200336 let lnum -= 1
337 endwhile
338 return 0
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200339 endif
340
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200341 if bb > 0
342 let ind = s:Get_last_paren_idx(getline(lnum), '[', ']', bb)
343 return ind
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200344 endif
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200345 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200346
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200347 let post_block = 0
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100348 if line =~ '}$' && s:Get_paren_balance(line, '{', '}') < 0
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200349 let lnum = s:Get_matching_brace(lnum, '{', '}', 0)
350 let line = SanitizeRLine(getline(lnum))
351 if lnum > 0 && line =~ '^\s*{'
352 let lnum = s:Get_prev_line(lnum)
353 let line = SanitizeRLine(getline(lnum))
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200354 endif
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200355 let pb = s:Get_paren_balance(line, '(', ')')
356 let post_block = 1
357 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200358
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200359 " Indent after operator pattern
360 let olnum = s:Get_prev_line(lnum)
361 let oline = getline(olnum)
362 if olnum > 0
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100363 if line =~ g:r_indent_op_pattern && s:Get_paren_balance(line, "(", ")") == 0
364 if oline =~ g:r_indent_op_pattern && s:Get_paren_balance(line, "(", ")") == 0
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200365 return indent(lnum)
366 else
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200367 return indent(lnum) + shiftwidth()
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200368 endif
369 else
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100370 if oline =~ g:r_indent_op_pattern && s:Get_paren_balance(line, "(", ")") == 0
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200371 return indent(lnum) - shiftwidth()
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200372 endif
Bram Moolenaar15146672011-10-20 22:22:38 +0200373 endif
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200374 endif
Bram Moolenaar15146672011-10-20 22:22:38 +0200375
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200376 let post_fun = 0
377 if pb < 0 && line !~ ')\s*[,&|\-\*+<>]$'
378 let post_fun = 1
379 while pb < 0 && lnum > 0
380 let lnum -= 1
381 let linepiece = SanitizeRLine(getline(lnum))
382 let pb += s:Get_paren_balance(linepiece, "(", ")")
383 let line = linepiece . line
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200384 endwhile
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200385 if line =~ '{$' && post_block == 0
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200386 return indent(lnum) + shiftwidth()
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200387 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200388
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200389 " Now we can do some tests again
390 if cline =~ '^\s*{'
391 return indent(lnum)
392 endif
393 if post_block == 0
394 let newl = SanitizeRLine(line)
395 if newl =~ '\<\(if\|while\|for\|function\)\s*()$' || newl =~ '\<else$' || newl =~ '<-$'
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200396 return indent(lnum) + shiftwidth()
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200397 endif
398 endif
399 endif
400
401 if cline =~ '^\s*else'
402 if line =~ '<-\s*if\s*()'
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200403 return indent(lnum) + shiftwidth()
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200404 else
405 if line =~ '\<if\s*()'
406 return indent(lnum)
407 else
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200408 return indent(lnum) - shiftwidth()
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200409 endif
410 endif
411 endif
412
413 let bb = s:Get_paren_balance(line, '[', ']')
414 if bb < 0 && line =~ '.*]'
415 while bb < 0 && lnum > 0
416 let lnum -= 1
417 let linepiece = SanitizeRLine(getline(lnum))
418 let bb += s:Get_paren_balance(linepiece, "[", "]")
419 let line = linepiece . line
420 endwhile
421 let line = s:RDelete_parens(line)
422 endif
423
424 let plnum = s:Get_prev_line(lnum)
425 let ppost_else = 0
426 if plnum > 0
427 let pline = SanitizeRLine(getline(plnum))
428 let ppost_block = 0
429 if pline =~ '}$'
430 let ppost_block = 1
431 let plnum = s:Get_matching_brace(plnum, '{', '}', 0)
432 let pline = SanitizeRLine(getline(plnum))
433 if pline =~ '^\s*{$' && plnum > 0
434 let plnum = s:Get_prev_line(plnum)
435 let pline = SanitizeRLine(getline(plnum))
436 endif
437 endif
438
439 if pline =~ 'else$'
440 let ppost_else = 1
441 let plnum = s:Get_matching_if(plnum, 0)
442 let pline = SanitizeRLine(getline(plnum))
443 endif
444
445 if pline =~ '^\s*else\s*if\s*('
446 let pplnum = s:Get_prev_line(plnum)
447 let ppline = SanitizeRLine(getline(pplnum))
448 while ppline =~ '^\s*else\s*if\s*(' || ppline =~ '^\s*if\s*()\s*\S$'
449 let plnum = pplnum
450 let pline = ppline
451 let pplnum = s:Get_prev_line(plnum)
452 let ppline = SanitizeRLine(getline(pplnum))
453 endwhile
454 while ppline =~ '\<\(if\|while\|for\|function\)\s*()$' || ppline =~ '\<else$' || ppline =~ '<-$'
455 let plnum = pplnum
456 let pline = ppline
457 let pplnum = s:Get_prev_line(plnum)
458 let ppline = SanitizeRLine(getline(pplnum))
459 endwhile
460 endif
461
462 let ppb = s:Get_paren_balance(pline, '(', ')')
463 if ppb < 0 && (pline =~ ')\s*{$' || pline =~ ')$')
464 while ppb < 0 && plnum > 0
465 let plnum -= 1
466 let linepiece = SanitizeRLine(getline(plnum))
467 let ppb += s:Get_paren_balance(linepiece, "(", ")")
468 let pline = linepiece . pline
469 endwhile
470 let pline = s:RDelete_parens(pline)
471 endif
472 endif
473
474 let ind = indent(lnum)
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200475
476 if g:r_indent_align_args == 0 && pb != 0
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200477 let ind += pb * shiftwidth()
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200478 return ind
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200479 endif
480
481 if g:r_indent_align_args == 0 && bb != 0
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200482 let ind += bb * shiftwidth()
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200483 return ind
484 endif
485
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100486 if plnum > 0
487 let pind = indent(plnum)
488 else
489 let pind = 0
490 endif
491
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200492 if ind == pind || (ind == (pind + shiftwidth()) && pline =~ '{$' && ppost_else == 0)
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200493 return ind
494 endif
495
496 let pline = getline(plnum)
497 let pbb = s:Get_paren_balance(pline, '[', ']')
498
499 while pind < ind && plnum > 0 && ppb == 0 && pbb == 0
500 let ind = pind
501 let plnum = s:Get_prev_line(plnum)
502 let pline = getline(plnum)
503 let ppb = s:Get_paren_balance(pline, '(', ')')
504 let pbb = s:Get_paren_balance(pline, '[', ']')
505 while pline =~ '^\s*else'
506 let plnum = s:Get_matching_if(plnum, 1)
507 let pline = getline(plnum)
508 let ppb = s:Get_paren_balance(pline, '(', ')')
509 let pbb = s:Get_paren_balance(pline, '[', ']')
510 endwhile
511 let pind = indent(plnum)
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200512 if ind == (pind + shiftwidth()) && pline =~ '{$'
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200513 return ind
514 endif
515 endwhile
516
517 return ind
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200518
519endfunction
520
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200521" vim: sw=2