blob: 339c46b17298e5375a928f2634dd4d9a1ac700c7 [file] [log] [blame]
Bram Moolenaar5302d9e2011-09-14 17:55:08 +02001" Vim indent file
2" Language: R
Christian Brabandtf9ca1392024-02-19 20:37:11 +01003" Maintainer: This runtime file is looking for a new maintainer.
4" Former Maintainer: Jakson Alves de Aquino <jalvesaq@gmail.com>
5" Former Repository: https://github.com/jalvesaq/R-Vim-runtime
6" Last Change: 2023 Oct 08 10:45AM
7" 2024 Feb 19 by Vim Project (announce adoption)
Bram Moolenaar5302d9e2011-09-14 17:55:08 +02008
9
10" Only load this indent file when no other was loaded.
Bram Moolenaar26402cb2013-02-20 21:26:00 +010011if exists("b:did_indent")
Bram Moolenaar541f92d2015-06-19 13:27:23 +020012 finish
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020013endif
Bram Moolenaar26402cb2013-02-20 21:26:00 +010014let b:did_indent = 1
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020015
16setlocal indentkeys=0{,0},:,!^F,o,O,e
17setlocal indentexpr=GetRIndent()
Jakson Alves de Aquino9042bd82023-12-25 09:22:27 +000018setlocal autoindent
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020019
Bram Moolenaardd60c362023-02-27 15:49:53 +000020let b:undo_indent = "setl inde< indk<"
21
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020022" Only define the function once.
23if exists("*GetRIndent")
Bram Moolenaar541f92d2015-06-19 13:27:23 +020024 finish
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020025endif
26
Bram Moolenaarfc65cab2018-08-28 22:58:02 +020027let s:cpo_save = &cpo
28set cpo&vim
29
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020030" Options to make the indentation more similar to Emacs/ESS:
Bram Moolenaarfc65cab2018-08-28 22:58:02 +020031let g:r_indent_align_args = get(g:, 'r_indent_align_args', 1)
32let g:r_indent_ess_comments = get(g:, 'r_indent_ess_comments', 0)
33let g:r_indent_comment_column = get(g:, 'r_indent_comment_column', 40)
34let g:r_indent_ess_compatible = get(g:, 'r_indent_ess_compatible', 0)
35let g:r_indent_op_pattern = get(g:, 'r_indent_op_pattern',
Bram Moolenaardd60c362023-02-27 15:49:53 +000036 \ '\(&\||\|+\|-\|\*\|/\|=\|\~\|%\|->\||>\)\s*$')
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020037
38function s:RDelete_quotes(line)
Bram Moolenaar541f92d2015-06-19 13:27:23 +020039 let i = 0
40 let j = 0
41 let line1 = ""
42 let llen = strlen(a:line)
43 while i < llen
44 if a:line[i] == '"'
45 let i += 1
46 let line1 = line1 . 's'
47 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 +020048 let i += 1
Bram Moolenaar541f92d2015-06-19 13:27:23 +020049 endwhile
50 if a:line[i] == '"'
51 let i += 1
52 endif
Jakson Alves de Aquino9042bd82023-12-25 09:22:27 +000053 elseif a:line[i] == "'"
54 let i += 1
55 let line1 = line1 . 's'
56 while !(a:line[i] == "'" && ((i > 1 && a:line[i-1] == '\' && a:line[i-2] == '\') || a:line[i-1] != '\')) && i < llen
57 let i += 1
58 endwhile
Bram Moolenaar541f92d2015-06-19 13:27:23 +020059 if a:line[i] == "'"
60 let i += 1
Jakson Alves de Aquino9042bd82023-12-25 09:22:27 +000061 endif
62 elseif a:line[i] == "`"
63 let i += 1
64 let line1 = line1 . 's'
65 while a:line[i] != "`" && i < llen
66 let i += 1
67 endwhile
68 if a:line[i] == "`"
69 let i += 1
Bram Moolenaar541f92d2015-06-19 13:27:23 +020070 endif
71 endif
72 if i == llen
73 break
74 endif
75 let line1 = line1 . a:line[i]
76 let j += 1
77 let i += 1
78 endwhile
79 return line1
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020080endfunction
81
82" Convert foo(bar()) int foo()
83function s:RDelete_parens(line)
Bram Moolenaar541f92d2015-06-19 13:27:23 +020084 if s:Get_paren_balance(a:line, "(", ")") != 0
85 return a:line
86 endif
87 let i = 0
88 let j = 0
89 let line1 = ""
90 let llen = strlen(a:line)
91 while i < llen
92 let line1 = line1 . a:line[i]
93 if a:line[i] == '('
94 let nop = 1
95 while nop > 0 && i < llen
Bram Moolenaar5302d9e2011-09-14 17:55:08 +020096 let i += 1
Bram Moolenaar541f92d2015-06-19 13:27:23 +020097 if a:line[i] == ')'
98 let nop -= 1
Jakson Alves de Aquino9042bd82023-12-25 09:22:27 +000099 elseif a:line[i] == '('
100 let nop += 1
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200101 endif
102 endwhile
103 let line1 = line1 . a:line[i]
104 endif
105 let i += 1
106 endwhile
107 return line1
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200108endfunction
109
Bram Moolenaar71badf92023-04-22 22:40:14 +0100110function s:Get_paren_balance(line, o, c)
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200111 let line2 = substitute(a:line, a:o, "", "g")
112 let openp = strlen(a:line) - strlen(line2)
113 let line3 = substitute(line2, a:c, "", "g")
114 let closep = strlen(line2) - strlen(line3)
115 return openp - closep
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200116endfunction
117
Bram Moolenaar71badf92023-04-22 22:40:14 +0100118function s:Get_matching_brace(linenr, o, c, delbrace)
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200119 let line = SanitizeRLine(getline(a:linenr))
120 if a:delbrace == 1
121 let line = substitute(line, '{$', "", "")
122 endif
123 let pb = s:Get_paren_balance(line, a:o, a:c)
124 let i = a:linenr
125 while pb != 0 && i > 1
126 let i -= 1
127 let pb += s:Get_paren_balance(SanitizeRLine(getline(i)), a:o, a:c)
128 endwhile
129 return i
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200130endfunction
131
132" This function is buggy because there 'if's without 'else'
133" It must be rewritten relying more on indentation
Bram Moolenaar71badf92023-04-22 22:40:14 +0100134function s:Get_matching_if(linenr, delif)
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200135 let line = SanitizeRLine(getline(a:linenr))
136 if a:delif
137 let line = substitute(line, "if", "", "g")
138 endif
139 let elsenr = 0
140 let i = a:linenr
141 let ifhere = 0
142 while i > 0
143 let line2 = substitute(line, '\<else\>', "xxx", "g")
144 let elsenr += strlen(line) - strlen(line2)
145 if line =~ '.*\s*if\s*()' || line =~ '.*\s*if\s*()'
146 let elsenr -= 1
147 if elsenr == 0
148 let ifhere = i
149 break
150 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200151 endif
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200152 let i -= 1
153 let line = SanitizeRLine(getline(i))
154 endwhile
155 if ifhere
156 return ifhere
157 else
158 return a:linenr
159 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200160endfunction
161
Bram Moolenaar71badf92023-04-22 22:40:14 +0100162function s:Get_last_paren_idx(line, o, c, pb)
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200163 let blc = a:pb
164 let line = substitute(a:line, '\t', s:curtabstop, "g")
165 let theidx = -1
166 let llen = strlen(line)
167 let idx = 0
168 while idx < llen
169 if line[idx] == a:o
170 let blc -= 1
171 if blc == 0
172 let theidx = idx
173 endif
Jakson Alves de Aquino9042bd82023-12-25 09:22:27 +0000174 elseif line[idx] == a:c
175 let blc += 1
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200176 endif
177 let idx += 1
178 endwhile
179 return theidx + 1
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200180endfunction
181
182" Get previous relevant line. Search back until getting a line that isn't
183" comment or blank
184function s:Get_prev_line(lineno)
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200185 let lnum = a:lineno - 1
186 let data = getline( lnum )
187 while lnum > 0 && (data =~ '^\s*#' || data =~ '^\s*$')
188 let lnum = lnum - 1
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200189 let data = getline( lnum )
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200190 endwhile
191 return lnum
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200192endfunction
193
194" This function is also used by r-plugin/common_global.vim
195" Delete from '#' to the end of the line, unless the '#' is inside a string.
196function SanitizeRLine(line)
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200197 let newline = s:RDelete_quotes(a:line)
198 let newline = s:RDelete_parens(newline)
199 let newline = substitute(newline, '#.*', "", "")
200 let newline = substitute(newline, '\s*$', "", "")
201 if &filetype == "rhelp" && newline =~ '^\\method{.*}{.*}(.*'
202 let newline = substitute(newline, '^\\method{\(.*\)}{.*}', '\1', "")
203 endif
204 return newline
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200205endfunction
206
207function GetRIndent()
208
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200209 let clnum = line(".") " current line
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200210
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200211 let cline = getline(clnum)
212 if cline =~ '^\s*#'
213 if g:r_indent_ess_comments == 1
214 if cline =~ '^\s*###'
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200215 return 0
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200216 endif
217 if cline !~ '^\s*##'
218 return g:r_indent_comment_column
219 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200220 endif
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200221 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200222
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200223 let cline = SanitizeRLine(cline)
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200224
Bram Moolenaarfc65cab2018-08-28 22:58:02 +0200225 if cline =~ '^\s*}'
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200226 let indline = s:Get_matching_brace(clnum, '{', '}', 1)
227 if indline > 0 && indline != clnum
228 let iline = SanitizeRLine(getline(indline))
229 if s:Get_paren_balance(iline, "(", ")") == 0 || iline =~ '(\s*{$'
230 return indent(indline)
231 else
232 let indline = s:Get_matching_brace(indline, '(', ')', 1)
233 return indent(indline)
234 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200235 endif
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200236 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200237
Bram Moolenaarfc65cab2018-08-28 22:58:02 +0200238 if cline =~ '^\s*)$'
239 let indline = s:Get_matching_brace(clnum, '(', ')', 1)
240 return indent(indline)
241 endif
242
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200243 " Find the first non blank line above the current line
244 let lnum = s:Get_prev_line(clnum)
245 " Hit the start of the file, use zero indent.
246 if lnum == 0
247 return 0
248 endif
249
250 let line = SanitizeRLine(getline(lnum))
251
252 if &filetype == "rhelp"
253 if cline =~ '^\\dontshow{' || cline =~ '^\\dontrun{' || cline =~ '^\\donttest{' || cline =~ '^\\testonly{'
254 return 0
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200255 endif
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200256 if line =~ '^\\examples{' || line =~ '^\\usage{' || line =~ '^\\dontshow{' || line =~ '^\\dontrun{' || line =~ '^\\donttest{' || line =~ '^\\testonly{'
257 return 0
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200258 endif
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200259 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200260
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200261 if &filetype == "rnoweb" && line =~ "^<<.*>>="
262 return 0
263 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200264
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100265 if cline =~ '^\s*{' && s:Get_paren_balance(cline, '{', '}') > 0
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200266 if g:r_indent_ess_compatible && line =~ ')$'
267 let nlnum = lnum
268 let nline = line
269 while s:Get_paren_balance(nline, '(', ')') < 0
270 let nlnum = s:Get_prev_line(nlnum)
271 let nline = SanitizeRLine(getline(nlnum)) . nline
272 endwhile
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200273 if nline =~ '^\s*function\s*(' && indent(nlnum) == shiftwidth()
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200274 return 0
275 endif
276 endif
277 if s:Get_paren_balance(line, "(", ")") == 0
278 return indent(lnum)
279 endif
280 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200281
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200282 " line is an incomplete command:
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100283 if line =~ '\<\(if\|while\|for\|function\)\s*()$' || line =~ '\<else$' || line =~ '<-$' || line =~ '->$'
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200284 return indent(lnum) + shiftwidth()
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200285 endif
286
287 " Deal with () and []
288
289 let pb = s:Get_paren_balance(line, '(', ')')
290
291 if line =~ '^\s*{$' || line =~ '(\s*{' || (pb == 0 && (line =~ '{$' || line =~ '(\s*{$'))
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200292 return indent(lnum) + shiftwidth()
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200293 endif
294
295 let s:curtabstop = repeat(' ', &tabstop)
296
297 if g:r_indent_align_args == 1
298 if pb > 0 && line =~ '{$'
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200299 return s:Get_last_paren_idx(line, '(', ')', pb) + shiftwidth()
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200300 endif
301
302 let bb = s:Get_paren_balance(line, '[', ']')
303
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200304 if pb > 0
305 if &filetype == "rhelp"
306 let ind = s:Get_last_paren_idx(line, '(', ')', pb)
307 else
308 let ind = s:Get_last_paren_idx(getline(lnum), '(', ')', pb)
309 endif
310 return ind
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200311 endif
312
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200313 if pb < 0 && line =~ '.*[,&|\-\*+<>]$'
Jakson Alves de Aquino9042bd82023-12-25 09:22:27 +0000314 if line =~ '.*[\-\*+>]$'
315 let is_op = v:true
316 else
317 let is_op = v:false
318 endif
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200319 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
Jakson Alves de Aquino9042bd82023-12-25 09:22:27 +0000327 elseif line[ind] == '('
328 let pb += 1
329 if is_op && pb == 0
330 return indent(lnum)
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200331 endif
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200332 endif
333 if pb == 1
334 return ind + 1
335 endif
336 let ind -= 1
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200337 endwhile
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200338 let lnum -= 1
339 endwhile
340 return 0
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200341 endif
342
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200343 if bb > 0
344 let ind = s:Get_last_paren_idx(getline(lnum), '[', ']', bb)
345 return ind
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200346 endif
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200347 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200348
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200349 let post_block = 0
Bram Moolenaar77cdfd12016-03-12 12:57:59 +0100350 if line =~ '}$' && s:Get_paren_balance(line, '{', '}') < 0
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200351 let lnum = s:Get_matching_brace(lnum, '{', '}', 0)
352 let line = SanitizeRLine(getline(lnum))
353 if lnum > 0 && line =~ '^\s*{'
354 let lnum = s:Get_prev_line(lnum)
355 let line = SanitizeRLine(getline(lnum))
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200356 endif
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200357 let pb = s:Get_paren_balance(line, '(', ')')
358 let post_block = 1
359 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200360
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200361 " Indent after operator pattern
362 let olnum = s:Get_prev_line(lnum)
363 let oline = getline(olnum)
364 if olnum > 0
Bram Moolenaardd60c362023-02-27 15:49:53 +0000365 if substitute(line, '#.*', '', '') =~ g:r_indent_op_pattern && s:Get_paren_balance(line, "(", ")") == 0
366 if substitute(oline, '#.*', '', '') =~ g:r_indent_op_pattern && s:Get_paren_balance(line, "(", ")") == 0
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200367 return indent(lnum)
368 else
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200369 return indent(lnum) + shiftwidth()
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200370 endif
Jakson Alves de Aquino9042bd82023-12-25 09:22:27 +0000371 elseif substitute(oline, '#.*', '', '') =~ g:r_indent_op_pattern && s:Get_paren_balance(line, "(", ")") == 0
372 return indent(lnum) - shiftwidth()
Bram Moolenaar15146672011-10-20 22:22:38 +0200373 endif
Bram Moolenaardd60c362023-02-27 15:49:53 +0000374 elseif substitute(line, '#.*', '', '') =~ g:r_indent_op_pattern && s:Get_paren_balance(line, "(", ")") == 0
375 return indent(lnum) + shiftwidth()
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200376 endif
Bram Moolenaar15146672011-10-20 22:22:38 +0200377
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200378 let post_fun = 0
379 if pb < 0 && line !~ ')\s*[,&|\-\*+<>]$'
380 let post_fun = 1
381 while pb < 0 && lnum > 0
382 let lnum -= 1
383 let linepiece = SanitizeRLine(getline(lnum))
384 let pb += s:Get_paren_balance(linepiece, "(", ")")
385 let line = linepiece . line
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200386 endwhile
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200387 if line =~ '{$' && post_block == 0
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200388 return indent(lnum) + shiftwidth()
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200389 endif
Bram Moolenaar5302d9e2011-09-14 17:55:08 +0200390
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200391 " Now we can do some tests again
392 if cline =~ '^\s*{'
393 return indent(lnum)
394 endif
395 if post_block == 0
396 let newl = SanitizeRLine(line)
397 if newl =~ '\<\(if\|while\|for\|function\)\s*()$' || newl =~ '\<else$' || newl =~ '<-$'
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200398 return indent(lnum) + shiftwidth()
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200399 endif
400 endif
401 endif
402
403 if cline =~ '^\s*else'
404 if line =~ '<-\s*if\s*()'
Bram Moolenaarcd5c8f82017-04-09 20:11:58 +0200405 return indent(lnum) + shiftwidth()
Jakson Alves de Aquino9042bd82023-12-25 09:22:27 +0000406 elseif line =~ '\<if\s*()'
407 return indent(lnum)
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200408 else
Jakson Alves de Aquino9042bd82023-12-25 09:22:27 +0000409 return indent(lnum) - shiftwidth()
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200410 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 +0200518endfunction
519
Bram Moolenaarfc65cab2018-08-28 22:58:02 +0200520let &cpo = s:cpo_save
521unlet s:cpo_save
522
Bram Moolenaar541f92d2015-06-19 13:27:23 +0200523" vim: sw=2