blob: 293dd9817583dc807ba511231f1b3849aabed3dd [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
Bram Moolenaardd60c362023-02-27 15:49:53 +00005" Last Change: Wed Oct 26, 2022 12:04PM
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
Bram Moolenaardd60c362023-02-27 15:49:53 +000017let b:undo_indent = "setl inde< indk<"
18
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020019" Only define the function once.
20if exists("*GetRIndent")
Bram Moolenaar541f92d2015-06-19 13:27:23 +020021 finish
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020022endif
23
Bram Moolenaarfc65cab2018-08-28 22:58:02 +020024let s:cpo_save = &cpo
25set cpo&vim
26
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020027" Options to make the indentation more similar to Emacs/ESS:
Bram Moolenaarfc65cab2018-08-28 22:58:02 +020028let g:r_indent_align_args = get(g:, 'r_indent_align_args', 1)
29let g:r_indent_ess_comments = get(g:, 'r_indent_ess_comments', 0)
30let g:r_indent_comment_column = get(g:, 'r_indent_comment_column', 40)
31let g:r_indent_ess_compatible = get(g:, 'r_indent_ess_compatible', 0)
32let g:r_indent_op_pattern = get(g:, 'r_indent_op_pattern',
Bram Moolenaardd60c362023-02-27 15:49:53 +000033 \ '\(&\||\|+\|-\|\*\|/\|=\|\~\|%\|->\||>\)\s*$')
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020034
35function s:RDelete_quotes(line)
Bram Moolenaar541f92d2015-06-19 13:27:23 +020036 let i = 0
37 let j = 0
38 let line1 = ""
39 let llen = strlen(a:line)
40 while i < llen
41 if a:line[i] == '"'
42 let i += 1
43 let line1 = line1 . 's'
44 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 +020045 let i += 1
Bram Moolenaar541f92d2015-06-19 13:27:23 +020046 endwhile
47 if a:line[i] == '"'
48 let i += 1
49 endif
50 else
51 if a:line[i] == "'"
52 let i += 1
53 let line1 = line1 . 's'
54 while !(a:line[i] == "'" && ((i > 1 && a:line[i-1] == '\' && a:line[i-2] == '\') || a:line[i-1] != '\')) && i < llen
55 let i += 1
56 endwhile
57 if a:line[i] == "'"
58 let i += 1
59 endif
60 else
61 if a:line[i] == "`"
62 let i += 1
63 let line1 = line1 . 's'
64 while a:line[i] != "`" && i < llen
65 let i += 1
66 endwhile
67 if a:line[i] == "`"
68 let i += 1
69 endif
70 endif
71 endif
72 endif
73 if i == llen
74 break
75 endif
76 let line1 = line1 . a:line[i]
77 let j += 1
78 let i += 1
79 endwhile
80 return line1
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020081endfunction
82
83" Convert foo(bar()) int foo()
84function s:RDelete_parens(line)
Bram Moolenaar541f92d2015-06-19 13:27:23 +020085 if s:Get_paren_balance(a:line, "(", ")") != 0
86 return a:line
87 endif
88 let i = 0
89 let j = 0
90 let line1 = ""
91 let llen = strlen(a:line)
92 while i < llen
93 let line1 = line1 . a:line[i]
94 if a:line[i] == '('
95 let nop = 1
96 while nop > 0 && i < llen
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020097 let i += 1
Bram Moolenaar541f92d2015-06-19 13:27:23 +020098 if a:line[i] == ')'
99 let nop -= 1
100 else
101 if a:line[i] == '('
102 let nop += 1
103 endif
104 endif
105 endwhile
106 let line1 = line1 . a:line[i]
107 endif
108 let i += 1
109 endwhile
110 return line1
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200111endfunction
112
113function! s:Get_paren_balance(line, o, c)
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200114 let line2 = substitute(a:line, a:o, "", "g")
115 let openp = strlen(a:line) - strlen(line2)
116 let line3 = substitute(line2, a:c, "", "g")
117 let closep = strlen(line2) - strlen(line3)
118 return openp - closep
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200119endfunction
120
121function! s:Get_matching_brace(linenr, o, c, delbrace)
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200122 let line = SanitizeRLine(getline(a:linenr))
123 if a:delbrace == 1
124 let line = substitute(line, '{$', "", "")
125 endif
126 let pb = s:Get_paren_balance(line, a:o, a:c)
127 let i = a:linenr
128 while pb != 0 && i > 1
129 let i -= 1
130 let pb += s:Get_paren_balance(SanitizeRLine(getline(i)), a:o, a:c)
131 endwhile
132 return i
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200133endfunction
134
135" This function is buggy because there 'if's without 'else'
136" It must be rewritten relying more on indentation
137function! s:Get_matching_if(linenr, delif)
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200138 let line = SanitizeRLine(getline(a:linenr))
139 if a:delif
140 let line = substitute(line, "if", "", "g")
141 endif
142 let elsenr = 0
143 let i = a:linenr
144 let ifhere = 0
145 while i > 0
146 let line2 = substitute(line, '\<else\>', "xxx", "g")
147 let elsenr += strlen(line) - strlen(line2)
148 if line =~ '.*\s*if\s*()' || line =~ '.*\s*if\s*()'
149 let elsenr -= 1
150 if elsenr == 0
151 let ifhere = i
152 break
153 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200154 endif
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200155 let i -= 1
156 let line = SanitizeRLine(getline(i))
157 endwhile
158 if ifhere
159 return ifhere
160 else
161 return a:linenr
162 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200163endfunction
164
165function! s:Get_last_paren_idx(line, o, c, pb)
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200166 let blc = a:pb
167 let line = substitute(a:line, '\t', s:curtabstop, "g")
168 let theidx = -1
169 let llen = strlen(line)
170 let idx = 0
171 while idx < llen
172 if line[idx] == a:o
173 let blc -= 1
174 if blc == 0
175 let theidx = idx
176 endif
177 else
178 if line[idx] == a:c
179 let blc += 1
180 endif
181 endif
182 let idx += 1
183 endwhile
184 return theidx + 1
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200185endfunction
186
187" Get previous relevant line. Search back until getting a line that isn't
188" comment or blank
189function s:Get_prev_line(lineno)
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200190 let lnum = a:lineno - 1
191 let data = getline( lnum )
192 while lnum > 0 && (data =~ '^\s*#' || data =~ '^\s*$')
193 let lnum = lnum - 1
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200194 let data = getline( lnum )
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200195 endwhile
196 return lnum
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200197endfunction
198
199" This function is also used by r-plugin/common_global.vim
200" Delete from '#' to the end of the line, unless the '#' is inside a string.
201function SanitizeRLine(line)
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200202 let newline = s:RDelete_quotes(a:line)
203 let newline = s:RDelete_parens(newline)
204 let newline = substitute(newline, '#.*', "", "")
205 let newline = substitute(newline, '\s*$', "", "")
206 if &filetype == "rhelp" && newline =~ '^\\method{.*}{.*}(.*'
207 let newline = substitute(newline, '^\\method{\(.*\)}{.*}', '\1', "")
208 endif
209 return newline
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200210endfunction
211
212function GetRIndent()
213
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200214 let clnum = line(".") " current line
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200215
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200216 let cline = getline(clnum)
217 if cline =~ '^\s*#'
218 if g:r_indent_ess_comments == 1
219 if cline =~ '^\s*###'
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200220 return 0
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200221 endif
222 if cline !~ '^\s*##'
223 return g:r_indent_comment_column
224 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200225 endif
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200226 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200227
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200228 let cline = SanitizeRLine(cline)
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200229
Bram Moolenaarfc65cab2018-08-28 22:58:02 +0200230 if cline =~ '^\s*}'
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200231 let indline = s:Get_matching_brace(clnum, '{', '}', 1)
232 if indline > 0 && indline != clnum
233 let iline = SanitizeRLine(getline(indline))
234 if s:Get_paren_balance(iline, "(", ")") == 0 || iline =~ '(\s*{$'
235 return indent(indline)
236 else
237 let indline = s:Get_matching_brace(indline, '(', ')', 1)
238 return indent(indline)
239 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200240 endif
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200241 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200242
Bram Moolenaarfc65cab2018-08-28 22:58:02 +0200243 if cline =~ '^\s*)$'
244 let indline = s:Get_matching_brace(clnum, '(', ')', 1)
245 return indent(indline)
246 endif
247
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200248 " Find the first non blank line above the current line
249 let lnum = s:Get_prev_line(clnum)
250 " Hit the start of the file, use zero indent.
251 if lnum == 0
252 return 0
253 endif
254
255 let line = SanitizeRLine(getline(lnum))
256
257 if &filetype == "rhelp"
258 if cline =~ '^\\dontshow{' || cline =~ '^\\dontrun{' || cline =~ '^\\donttest{' || cline =~ '^\\testonly{'
259 return 0
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200260 endif
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200261 if line =~ '^\\examples{' || line =~ '^\\usage{' || line =~ '^\\dontshow{' || line =~ '^\\dontrun{' || line =~ '^\\donttest{' || line =~ '^\\testonly{'
262 return 0
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200263 endif
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200264 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200265
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200266 if &filetype == "rnoweb" && line =~ "^<<.*>>="
267 return 0
268 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200269
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100270 if cline =~ '^\s*{' && s:Get_paren_balance(cline, '{', '}') > 0
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200271 if g:r_indent_ess_compatible && line =~ ')$'
272 let nlnum = lnum
273 let nline = line
274 while s:Get_paren_balance(nline, '(', ')') < 0
275 let nlnum = s:Get_prev_line(nlnum)
276 let nline = SanitizeRLine(getline(nlnum)) . nline
277 endwhile
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200278 if nline =~ '^\s*function\s*(' && indent(nlnum) == shiftwidth()
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200279 return 0
280 endif
281 endif
282 if s:Get_paren_balance(line, "(", ")") == 0
283 return indent(lnum)
284 endif
285 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200286
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200287 " line is an incomplete command:
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100288 if line =~ '\<\(if\|while\|for\|function\)\s*()$' || line =~ '\<else$' || line =~ '<-$' || line =~ '->$'
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200289 return indent(lnum) + shiftwidth()
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200290 endif
291
292 " Deal with () and []
293
294 let pb = s:Get_paren_balance(line, '(', ')')
295
296 if line =~ '^\s*{$' || line =~ '(\s*{' || (pb == 0 && (line =~ '{$' || line =~ '(\s*{$'))
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200297 return indent(lnum) + shiftwidth()
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200298 endif
299
300 let s:curtabstop = repeat(' ', &tabstop)
301
302 if g:r_indent_align_args == 1
303 if pb > 0 && line =~ '{$'
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200304 return s:Get_last_paren_idx(line, '(', ')', pb) + shiftwidth()
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200305 endif
306
307 let bb = s:Get_paren_balance(line, '[', ']')
308
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200309 if pb > 0
310 if &filetype == "rhelp"
311 let ind = s:Get_last_paren_idx(line, '(', ')', pb)
312 else
313 let ind = s:Get_last_paren_idx(getline(lnum), '(', ')', pb)
314 endif
315 return ind
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200316 endif
317
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200318 if pb < 0 && line =~ '.*[,&|\-\*+<>]$'
319 let lnum = s:Get_prev_line(lnum)
320 while pb < 1 && lnum > 0
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200321 let line = SanitizeRLine(getline(lnum))
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200322 let line = substitute(line, '\t', s:curtabstop, "g")
323 let ind = strlen(line)
324 while ind > 0
325 if line[ind] == ')'
326 let pb -= 1
327 else
328 if line[ind] == '('
329 let pb += 1
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200330 endif
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200331 endif
332 if pb == 1
333 return ind + 1
334 endif
335 let ind -= 1
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200336 endwhile
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200337 let lnum -= 1
338 endwhile
339 return 0
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200340 endif
341
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200342 if bb > 0
343 let ind = s:Get_last_paren_idx(getline(lnum), '[', ']', bb)
344 return ind
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200345 endif
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200346 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200347
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200348 let post_block = 0
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100349 if line =~ '}$' && s:Get_paren_balance(line, '{', '}') < 0
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200350 let lnum = s:Get_matching_brace(lnum, '{', '}', 0)
351 let line = SanitizeRLine(getline(lnum))
352 if lnum > 0 && line =~ '^\s*{'
353 let lnum = s:Get_prev_line(lnum)
354 let line = SanitizeRLine(getline(lnum))
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200355 endif
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200356 let pb = s:Get_paren_balance(line, '(', ')')
357 let post_block = 1
358 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200359
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200360 " Indent after operator pattern
361 let olnum = s:Get_prev_line(lnum)
362 let oline = getline(olnum)
363 if olnum > 0
Bram Moolenaardd60c362023-02-27 15:49:53 +0000364 if substitute(line, '#.*', '', '') =~ g:r_indent_op_pattern && s:Get_paren_balance(line, "(", ")") == 0
365 if substitute(oline, '#.*', '', '') =~ g:r_indent_op_pattern && s:Get_paren_balance(line, "(", ")") == 0
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200366 return indent(lnum)
367 else
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200368 return indent(lnum) + shiftwidth()
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200369 endif
370 else
Bram Moolenaardd60c362023-02-27 15:49:53 +0000371 if substitute(oline, '#.*', '', '') =~ g:r_indent_op_pattern && s:Get_paren_balance(line, "(", ")") == 0
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200372 return indent(lnum) - shiftwidth()
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200373 endif
Bram Moolenaar15146672011-10-20 22:22:38 +0200374 endif
Bram Moolenaardd60c362023-02-27 15:49:53 +0000375 elseif substitute(line, '#.*', '', '') =~ g:r_indent_op_pattern && s:Get_paren_balance(line, "(", ")") == 0
376 return indent(lnum) + shiftwidth()
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200377 endif
Bram Moolenaar15146672011-10-20 22:22:38 +0200378
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200379 let post_fun = 0
380 if pb < 0 && line !~ ')\s*[,&|\-\*+<>]$'
381 let post_fun = 1
382 while pb < 0 && lnum > 0
383 let lnum -= 1
384 let linepiece = SanitizeRLine(getline(lnum))
385 let pb += s:Get_paren_balance(linepiece, "(", ")")
386 let line = linepiece . line
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200387 endwhile
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200388 if line =~ '{$' && post_block == 0
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200389 return indent(lnum) + shiftwidth()
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200390 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200391
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200392 " Now we can do some tests again
393 if cline =~ '^\s*{'
394 return indent(lnum)
395 endif
396 if post_block == 0
397 let newl = SanitizeRLine(line)
398 if newl =~ '\<\(if\|while\|for\|function\)\s*()$' || newl =~ '\<else$' || newl =~ '<-$'
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200399 return indent(lnum) + shiftwidth()
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200400 endif
401 endif
402 endif
403
404 if cline =~ '^\s*else'
405 if line =~ '<-\s*if\s*()'
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200406 return indent(lnum) + shiftwidth()
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200407 else
408 if line =~ '\<if\s*()'
409 return indent(lnum)
410 else
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200411 return indent(lnum) - shiftwidth()
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200412 endif
413 endif
414 endif
415
416 let bb = s:Get_paren_balance(line, '[', ']')
417 if bb < 0 && line =~ '.*]'
418 while bb < 0 && lnum > 0
419 let lnum -= 1
420 let linepiece = SanitizeRLine(getline(lnum))
421 let bb += s:Get_paren_balance(linepiece, "[", "]")
422 let line = linepiece . line
423 endwhile
424 let line = s:RDelete_parens(line)
425 endif
426
427 let plnum = s:Get_prev_line(lnum)
428 let ppost_else = 0
429 if plnum > 0
430 let pline = SanitizeRLine(getline(plnum))
431 let ppost_block = 0
432 if pline =~ '}$'
433 let ppost_block = 1
434 let plnum = s:Get_matching_brace(plnum, '{', '}', 0)
435 let pline = SanitizeRLine(getline(plnum))
436 if pline =~ '^\s*{$' && plnum > 0
437 let plnum = s:Get_prev_line(plnum)
438 let pline = SanitizeRLine(getline(plnum))
439 endif
440 endif
441
442 if pline =~ 'else$'
443 let ppost_else = 1
444 let plnum = s:Get_matching_if(plnum, 0)
445 let pline = SanitizeRLine(getline(plnum))
446 endif
447
448 if pline =~ '^\s*else\s*if\s*('
449 let pplnum = s:Get_prev_line(plnum)
450 let ppline = SanitizeRLine(getline(pplnum))
451 while ppline =~ '^\s*else\s*if\s*(' || ppline =~ '^\s*if\s*()\s*\S$'
452 let plnum = pplnum
453 let pline = ppline
454 let pplnum = s:Get_prev_line(plnum)
455 let ppline = SanitizeRLine(getline(pplnum))
456 endwhile
457 while ppline =~ '\<\(if\|while\|for\|function\)\s*()$' || ppline =~ '\<else$' || ppline =~ '<-$'
458 let plnum = pplnum
459 let pline = ppline
460 let pplnum = s:Get_prev_line(plnum)
461 let ppline = SanitizeRLine(getline(pplnum))
462 endwhile
463 endif
464
465 let ppb = s:Get_paren_balance(pline, '(', ')')
466 if ppb < 0 && (pline =~ ')\s*{$' || pline =~ ')$')
467 while ppb < 0 && plnum > 0
468 let plnum -= 1
469 let linepiece = SanitizeRLine(getline(plnum))
470 let ppb += s:Get_paren_balance(linepiece, "(", ")")
471 let pline = linepiece . pline
472 endwhile
473 let pline = s:RDelete_parens(pline)
474 endif
475 endif
476
477 let ind = indent(lnum)
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200478
479 if g:r_indent_align_args == 0 && pb != 0
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200480 let ind += pb * shiftwidth()
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200481 return ind
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200482 endif
483
484 if g:r_indent_align_args == 0 && bb != 0
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200485 let ind += bb * shiftwidth()
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200486 return ind
487 endif
488
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100489 if plnum > 0
490 let pind = indent(plnum)
491 else
492 let pind = 0
493 endif
494
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200495 if ind == pind || (ind == (pind + shiftwidth()) && pline =~ '{$' && ppost_else == 0)
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200496 return ind
497 endif
498
499 let pline = getline(plnum)
500 let pbb = s:Get_paren_balance(pline, '[', ']')
501
502 while pind < ind && plnum > 0 && ppb == 0 && pbb == 0
503 let ind = pind
504 let plnum = s:Get_prev_line(plnum)
505 let pline = getline(plnum)
506 let ppb = s:Get_paren_balance(pline, '(', ')')
507 let pbb = s:Get_paren_balance(pline, '[', ']')
508 while pline =~ '^\s*else'
509 let plnum = s:Get_matching_if(plnum, 1)
510 let pline = getline(plnum)
511 let ppb = s:Get_paren_balance(pline, '(', ')')
512 let pbb = s:Get_paren_balance(pline, '[', ']')
513 endwhile
514 let pind = indent(plnum)
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200515 if ind == (pind + shiftwidth()) && pline =~ '{$'
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200516 return ind
517 endif
518 endwhile
519
520 return ind
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200521endfunction
522
Bram Moolenaarfc65cab2018-08-28 22:58:02 +0200523let &cpo = s:cpo_save
524unlet s:cpo_save
525
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200526" vim: sw=2