blob: ca85a2e62d4e96d08c03209ad21e02e959858942 [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 Moolenaarfc65cab2018-08-28 22:58:02 +02005" Last Change: Sun Aug 19, 2018 09:13PM
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
Bram Moolenaarfc65cab2018-08-28 22:58:02 +020022let s:cpo_save = &cpo
23set cpo&vim
24
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020025" Options to make the indentation more similar to Emacs/ESS:
Bram Moolenaarfc65cab2018-08-28 22:58:02 +020026let g:r_indent_align_args = get(g:, 'r_indent_align_args', 1)
27let g:r_indent_ess_comments = get(g:, 'r_indent_ess_comments', 0)
28let g:r_indent_comment_column = get(g:, 'r_indent_comment_column', 40)
29let g:r_indent_ess_compatible = get(g:, 'r_indent_ess_compatible', 0)
30let g:r_indent_op_pattern = get(g:, 'r_indent_op_pattern',
31 \ '\(&\||\|+\|-\|\*\|/\|=\|\~\|%\|->\)\s*$')
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020032
33function s:RDelete_quotes(line)
Bram Moolenaar541f92d2015-06-19 13:27:23 +020034 let i = 0
35 let j = 0
36 let line1 = ""
37 let llen = strlen(a:line)
38 while i < llen
39 if a:line[i] == '"'
40 let i += 1
41 let line1 = line1 . 's'
42 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 +020043 let i += 1
Bram Moolenaar541f92d2015-06-19 13:27:23 +020044 endwhile
45 if a:line[i] == '"'
46 let i += 1
47 endif
48 else
49 if a:line[i] == "'"
50 let i += 1
51 let line1 = line1 . 's'
52 while !(a:line[i] == "'" && ((i > 1 && a:line[i-1] == '\' && a:line[i-2] == '\') || a:line[i-1] != '\')) && i < llen
53 let i += 1
54 endwhile
55 if a:line[i] == "'"
56 let i += 1
57 endif
58 else
59 if a:line[i] == "`"
60 let i += 1
61 let line1 = line1 . 's'
62 while a:line[i] != "`" && i < llen
63 let i += 1
64 endwhile
65 if a:line[i] == "`"
66 let i += 1
67 endif
68 endif
69 endif
70 endif
71 if i == llen
72 break
73 endif
74 let line1 = line1 . a:line[i]
75 let j += 1
76 let i += 1
77 endwhile
78 return line1
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020079endfunction
80
81" Convert foo(bar()) int foo()
82function s:RDelete_parens(line)
Bram Moolenaar541f92d2015-06-19 13:27:23 +020083 if s:Get_paren_balance(a:line, "(", ")") != 0
84 return a:line
85 endif
86 let i = 0
87 let j = 0
88 let line1 = ""
89 let llen = strlen(a:line)
90 while i < llen
91 let line1 = line1 . a:line[i]
92 if a:line[i] == '('
93 let nop = 1
94 while nop > 0 && i < llen
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020095 let i += 1
Bram Moolenaar541f92d2015-06-19 13:27:23 +020096 if a:line[i] == ')'
97 let nop -= 1
98 else
99 if a:line[i] == '('
100 let nop += 1
101 endif
102 endif
103 endwhile
104 let line1 = line1 . a:line[i]
105 endif
106 let i += 1
107 endwhile
108 return line1
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200109endfunction
110
111function! s:Get_paren_balance(line, o, c)
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200112 let line2 = substitute(a:line, a:o, "", "g")
113 let openp = strlen(a:line) - strlen(line2)
114 let line3 = substitute(line2, a:c, "", "g")
115 let closep = strlen(line2) - strlen(line3)
116 return openp - closep
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200117endfunction
118
119function! s:Get_matching_brace(linenr, o, c, delbrace)
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200120 let line = SanitizeRLine(getline(a:linenr))
121 if a:delbrace == 1
122 let line = substitute(line, '{$', "", "")
123 endif
124 let pb = s:Get_paren_balance(line, a:o, a:c)
125 let i = a:linenr
126 while pb != 0 && i > 1
127 let i -= 1
128 let pb += s:Get_paren_balance(SanitizeRLine(getline(i)), a:o, a:c)
129 endwhile
130 return i
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200131endfunction
132
133" This function is buggy because there 'if's without 'else'
134" It must be rewritten relying more on indentation
135function! s:Get_matching_if(linenr, delif)
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200136 let line = SanitizeRLine(getline(a:linenr))
137 if a:delif
138 let line = substitute(line, "if", "", "g")
139 endif
140 let elsenr = 0
141 let i = a:linenr
142 let ifhere = 0
143 while i > 0
144 let line2 = substitute(line, '\<else\>', "xxx", "g")
145 let elsenr += strlen(line) - strlen(line2)
146 if line =~ '.*\s*if\s*()' || line =~ '.*\s*if\s*()'
147 let elsenr -= 1
148 if elsenr == 0
149 let ifhere = i
150 break
151 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200152 endif
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200153 let i -= 1
154 let line = SanitizeRLine(getline(i))
155 endwhile
156 if ifhere
157 return ifhere
158 else
159 return a:linenr
160 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200161endfunction
162
163function! s:Get_last_paren_idx(line, o, c, pb)
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200164 let blc = a:pb
165 let line = substitute(a:line, '\t', s:curtabstop, "g")
166 let theidx = -1
167 let llen = strlen(line)
168 let idx = 0
169 while idx < llen
170 if line[idx] == a:o
171 let blc -= 1
172 if blc == 0
173 let theidx = idx
174 endif
175 else
176 if line[idx] == a:c
177 let blc += 1
178 endif
179 endif
180 let idx += 1
181 endwhile
182 return theidx + 1
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200183endfunction
184
185" Get previous relevant line. Search back until getting a line that isn't
186" comment or blank
187function s:Get_prev_line(lineno)
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200188 let lnum = a:lineno - 1
189 let data = getline( lnum )
190 while lnum > 0 && (data =~ '^\s*#' || data =~ '^\s*$')
191 let lnum = lnum - 1
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200192 let data = getline( lnum )
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200193 endwhile
194 return lnum
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200195endfunction
196
197" This function is also used by r-plugin/common_global.vim
198" Delete from '#' to the end of the line, unless the '#' is inside a string.
199function SanitizeRLine(line)
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200200 let newline = s:RDelete_quotes(a:line)
201 let newline = s:RDelete_parens(newline)
202 let newline = substitute(newline, '#.*', "", "")
203 let newline = substitute(newline, '\s*$', "", "")
204 if &filetype == "rhelp" && newline =~ '^\\method{.*}{.*}(.*'
205 let newline = substitute(newline, '^\\method{\(.*\)}{.*}', '\1', "")
206 endif
207 return newline
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200208endfunction
209
210function GetRIndent()
211
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200212 let clnum = line(".") " current line
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200213
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200214 let cline = getline(clnum)
215 if cline =~ '^\s*#'
216 if g:r_indent_ess_comments == 1
217 if cline =~ '^\s*###'
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200218 return 0
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200219 endif
220 if cline !~ '^\s*##'
221 return g:r_indent_comment_column
222 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200223 endif
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200224 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200225
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200226 let cline = SanitizeRLine(cline)
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200227
Bram Moolenaarfc65cab2018-08-28 22:58:02 +0200228 if cline =~ '^\s*}'
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200229 let indline = s:Get_matching_brace(clnum, '{', '}', 1)
230 if indline > 0 && indline != clnum
231 let iline = SanitizeRLine(getline(indline))
232 if s:Get_paren_balance(iline, "(", ")") == 0 || iline =~ '(\s*{$'
233 return indent(indline)
234 else
235 let indline = s:Get_matching_brace(indline, '(', ')', 1)
236 return indent(indline)
237 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200238 endif
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200239 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200240
Bram Moolenaarfc65cab2018-08-28 22:58:02 +0200241 if cline =~ '^\s*)$'
242 let indline = s:Get_matching_brace(clnum, '(', ')', 1)
243 return indent(indline)
244 endif
245
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200246 " Find the first non blank line above the current line
247 let lnum = s:Get_prev_line(clnum)
248 " Hit the start of the file, use zero indent.
249 if lnum == 0
250 return 0
251 endif
252
253 let line = SanitizeRLine(getline(lnum))
254
255 if &filetype == "rhelp"
256 if cline =~ '^\\dontshow{' || cline =~ '^\\dontrun{' || cline =~ '^\\donttest{' || cline =~ '^\\testonly{'
257 return 0
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200258 endif
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200259 if line =~ '^\\examples{' || line =~ '^\\usage{' || line =~ '^\\dontshow{' || line =~ '^\\dontrun{' || line =~ '^\\donttest{' || line =~ '^\\testonly{'
260 return 0
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200261 endif
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200262 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200263
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200264 if &filetype == "rnoweb" && line =~ "^<<.*>>="
265 return 0
266 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200267
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100268 if cline =~ '^\s*{' && s:Get_paren_balance(cline, '{', '}') > 0
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200269 if g:r_indent_ess_compatible && line =~ ')$'
270 let nlnum = lnum
271 let nline = line
272 while s:Get_paren_balance(nline, '(', ')') < 0
273 let nlnum = s:Get_prev_line(nlnum)
274 let nline = SanitizeRLine(getline(nlnum)) . nline
275 endwhile
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200276 if nline =~ '^\s*function\s*(' && indent(nlnum) == shiftwidth()
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200277 return 0
278 endif
279 endif
280 if s:Get_paren_balance(line, "(", ")") == 0
281 return indent(lnum)
282 endif
283 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200284
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200285 " line is an incomplete command:
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100286 if line =~ '\<\(if\|while\|for\|function\)\s*()$' || line =~ '\<else$' || line =~ '<-$' || line =~ '->$'
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200287 return indent(lnum) + shiftwidth()
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200288 endif
289
290 " Deal with () and []
291
292 let pb = s:Get_paren_balance(line, '(', ')')
293
294 if line =~ '^\s*{$' || line =~ '(\s*{' || (pb == 0 && (line =~ '{$' || line =~ '(\s*{$'))
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200295 return indent(lnum) + shiftwidth()
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200296 endif
297
298 let s:curtabstop = repeat(' ', &tabstop)
299
300 if g:r_indent_align_args == 1
301 if pb > 0 && line =~ '{$'
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200302 return s:Get_last_paren_idx(line, '(', ')', pb) + shiftwidth()
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200303 endif
304
305 let bb = s:Get_paren_balance(line, '[', ']')
306
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200307 if pb > 0
308 if &filetype == "rhelp"
309 let ind = s:Get_last_paren_idx(line, '(', ')', pb)
310 else
311 let ind = s:Get_last_paren_idx(getline(lnum), '(', ')', pb)
312 endif
313 return ind
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200314 endif
315
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200316 if pb < 0 && line =~ '.*[,&|\-\*+<>]$'
317 let lnum = s:Get_prev_line(lnum)
318 while pb < 1 && lnum > 0
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200319 let line = SanitizeRLine(getline(lnum))
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200320 let line = substitute(line, '\t', s:curtabstop, "g")
321 let ind = strlen(line)
322 while ind > 0
323 if line[ind] == ')'
324 let pb -= 1
325 else
326 if line[ind] == '('
327 let pb += 1
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200328 endif
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200329 endif
330 if pb == 1
331 return ind + 1
332 endif
333 let ind -= 1
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200334 endwhile
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200335 let lnum -= 1
336 endwhile
337 return 0
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200338 endif
339
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200340 if bb > 0
341 let ind = s:Get_last_paren_idx(getline(lnum), '[', ']', bb)
342 return ind
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200343 endif
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200344 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200345
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200346 let post_block = 0
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100347 if line =~ '}$' && s:Get_paren_balance(line, '{', '}') < 0
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200348 let lnum = s:Get_matching_brace(lnum, '{', '}', 0)
349 let line = SanitizeRLine(getline(lnum))
350 if lnum > 0 && line =~ '^\s*{'
351 let lnum = s:Get_prev_line(lnum)
352 let line = SanitizeRLine(getline(lnum))
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200353 endif
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200354 let pb = s:Get_paren_balance(line, '(', ')')
355 let post_block = 1
356 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200357
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200358 " Indent after operator pattern
359 let olnum = s:Get_prev_line(lnum)
360 let oline = getline(olnum)
361 if olnum > 0
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100362 if line =~ g:r_indent_op_pattern && s:Get_paren_balance(line, "(", ")") == 0
363 if oline =~ g:r_indent_op_pattern && s:Get_paren_balance(line, "(", ")") == 0
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200364 return indent(lnum)
365 else
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200366 return indent(lnum) + shiftwidth()
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200367 endif
368 else
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100369 if oline =~ g:r_indent_op_pattern && s:Get_paren_balance(line, "(", ")") == 0
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200370 return indent(lnum) - shiftwidth()
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200371 endif
Bram Moolenaar15146672011-10-20 22:22:38 +0200372 endif
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200373 endif
Bram Moolenaar15146672011-10-20 22:22:38 +0200374
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200375 let post_fun = 0
376 if pb < 0 && line !~ ')\s*[,&|\-\*+<>]$'
377 let post_fun = 1
378 while pb < 0 && lnum > 0
379 let lnum -= 1
380 let linepiece = SanitizeRLine(getline(lnum))
381 let pb += s:Get_paren_balance(linepiece, "(", ")")
382 let line = linepiece . line
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200383 endwhile
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200384 if line =~ '{$' && post_block == 0
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200385 return indent(lnum) + shiftwidth()
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200386 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200387
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200388 " Now we can do some tests again
389 if cline =~ '^\s*{'
390 return indent(lnum)
391 endif
392 if post_block == 0
393 let newl = SanitizeRLine(line)
394 if newl =~ '\<\(if\|while\|for\|function\)\s*()$' || newl =~ '\<else$' || newl =~ '<-$'
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200395 return indent(lnum) + shiftwidth()
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200396 endif
397 endif
398 endif
399
400 if cline =~ '^\s*else'
401 if line =~ '<-\s*if\s*()'
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200402 return indent(lnum) + shiftwidth()
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200403 else
404 if line =~ '\<if\s*()'
405 return indent(lnum)
406 else
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200407 return indent(lnum) - shiftwidth()
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200408 endif
409 endif
410 endif
411
412 let bb = s:Get_paren_balance(line, '[', ']')
413 if bb < 0 && line =~ '.*]'
414 while bb < 0 && lnum > 0
415 let lnum -= 1
416 let linepiece = SanitizeRLine(getline(lnum))
417 let bb += s:Get_paren_balance(linepiece, "[", "]")
418 let line = linepiece . line
419 endwhile
420 let line = s:RDelete_parens(line)
421 endif
422
423 let plnum = s:Get_prev_line(lnum)
424 let ppost_else = 0
425 if plnum > 0
426 let pline = SanitizeRLine(getline(plnum))
427 let ppost_block = 0
428 if pline =~ '}$'
429 let ppost_block = 1
430 let plnum = s:Get_matching_brace(plnum, '{', '}', 0)
431 let pline = SanitizeRLine(getline(plnum))
432 if pline =~ '^\s*{$' && plnum > 0
433 let plnum = s:Get_prev_line(plnum)
434 let pline = SanitizeRLine(getline(plnum))
435 endif
436 endif
437
438 if pline =~ 'else$'
439 let ppost_else = 1
440 let plnum = s:Get_matching_if(plnum, 0)
441 let pline = SanitizeRLine(getline(plnum))
442 endif
443
444 if pline =~ '^\s*else\s*if\s*('
445 let pplnum = s:Get_prev_line(plnum)
446 let ppline = SanitizeRLine(getline(pplnum))
447 while ppline =~ '^\s*else\s*if\s*(' || ppline =~ '^\s*if\s*()\s*\S$'
448 let plnum = pplnum
449 let pline = ppline
450 let pplnum = s:Get_prev_line(plnum)
451 let ppline = SanitizeRLine(getline(pplnum))
452 endwhile
453 while ppline =~ '\<\(if\|while\|for\|function\)\s*()$' || ppline =~ '\<else$' || ppline =~ '<-$'
454 let plnum = pplnum
455 let pline = ppline
456 let pplnum = s:Get_prev_line(plnum)
457 let ppline = SanitizeRLine(getline(pplnum))
458 endwhile
459 endif
460
461 let ppb = s:Get_paren_balance(pline, '(', ')')
462 if ppb < 0 && (pline =~ ')\s*{$' || pline =~ ')$')
463 while ppb < 0 && plnum > 0
464 let plnum -= 1
465 let linepiece = SanitizeRLine(getline(plnum))
466 let ppb += s:Get_paren_balance(linepiece, "(", ")")
467 let pline = linepiece . pline
468 endwhile
469 let pline = s:RDelete_parens(pline)
470 endif
471 endif
472
473 let ind = indent(lnum)
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200474
475 if g:r_indent_align_args == 0 && pb != 0
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200476 let ind += pb * shiftwidth()
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200477 return ind
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200478 endif
479
480 if g:r_indent_align_args == 0 && bb != 0
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200481 let ind += bb * shiftwidth()
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200482 return ind
483 endif
484
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100485 if plnum > 0
486 let pind = indent(plnum)
487 else
488 let pind = 0
489 endif
490
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200491 if ind == pind || (ind == (pind + shiftwidth()) && pline =~ '{$' && ppost_else == 0)
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200492 return ind
493 endif
494
495 let pline = getline(plnum)
496 let pbb = s:Get_paren_balance(pline, '[', ']')
497
498 while pind < ind && plnum > 0 && ppb == 0 && pbb == 0
499 let ind = pind
500 let plnum = s:Get_prev_line(plnum)
501 let pline = getline(plnum)
502 let ppb = s:Get_paren_balance(pline, '(', ')')
503 let pbb = s:Get_paren_balance(pline, '[', ']')
504 while pline =~ '^\s*else'
505 let plnum = s:Get_matching_if(plnum, 1)
506 let pline = getline(plnum)
507 let ppb = s:Get_paren_balance(pline, '(', ')')
508 let pbb = s:Get_paren_balance(pline, '[', ']')
509 endwhile
510 let pind = indent(plnum)
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200511 if ind == (pind + shiftwidth()) && pline =~ '{$'
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200512 return ind
513 endif
514 endwhile
515
516 return ind
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200517endfunction
518
Bram Moolenaarfc65cab2018-08-28 22:58:02 +0200519let &cpo = s:cpo_save
520unlet s:cpo_save
521
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200522" vim: sw=2