comparison runtime/indent/r.vim @ 6840:37828f7503c7

Updated runtime files.
author Bram Moolenaar <bram@vim.org>
date Fri, 19 Jun 2015 13:27:23 +0200
parents 8b86b69546a9
children da01d5da2cfa
comparison
equal deleted inserted replaced
6839:ef1bb1f160c5 6840:37828f7503c7
1 " Vim indent file 1 " Vim indent file
2 " Language: R 2 " Language: R
3 " Author: Jakson Alves de Aquino <jalvesaq@gmail.com> 3 " Author: Jakson Alves de Aquino <jalvesaq@gmail.com>
4 " Last Change: Fri Feb 15, 2013 08:11PM 4 " Last Change: Thu Mar 26, 2015 05:36PM
5 5
6 6
7 " Only load this indent file when no other was loaded. 7 " Only load this indent file when no other was loaded.
8 if exists("b:did_indent") 8 if exists("b:did_indent")
9 finish 9 finish
10 endif 10 endif
11 let b:did_indent = 1 11 let b:did_indent = 1
12 12
13 setlocal indentkeys=0{,0},:,!^F,o,O,e 13 setlocal indentkeys=0{,0},:,!^F,o,O,e
14 setlocal indentexpr=GetRIndent() 14 setlocal indentexpr=GetRIndent()
15 15
16 " Only define the function once. 16 " Only define the function once.
17 if exists("*GetRIndent") 17 if exists("*GetRIndent")
18 finish 18 finish
19 endif 19 endif
20 20
21 " Options to make the indentation more similar to Emacs/ESS: 21 " Options to make the indentation more similar to Emacs/ESS:
22 if !exists("g:r_indent_align_args") 22 if !exists("g:r_indent_align_args")
23 let g:r_indent_align_args = 1 23 let g:r_indent_align_args = 1
24 endif 24 endif
25 if !exists("g:r_indent_ess_comments") 25 if !exists("g:r_indent_ess_comments")
26 let g:r_indent_ess_comments = 0 26 let g:r_indent_ess_comments = 0
27 endif 27 endif
28 if !exists("g:r_indent_comment_column") 28 if !exists("g:r_indent_comment_column")
29 let g:r_indent_comment_column = 40 29 let g:r_indent_comment_column = 40
30 endif 30 endif
31 if ! exists("g:r_indent_ess_compatible") 31 if ! exists("g:r_indent_ess_compatible")
32 let g:r_indent_ess_compatible = 0 32 let g:r_indent_ess_compatible = 0
33 endif
34 if ! exists("g:r_indent_op_pattern")
35 let g:r_indent_op_pattern = '\(+\|-\|\*\|/\|=\|\~\|%\)$'
33 endif 36 endif
34 37
35 function s:RDelete_quotes(line) 38 function s:RDelete_quotes(line)
36 let i = 0 39 let i = 0
37 let j = 0 40 let j = 0
38 let line1 = "" 41 let line1 = ""
39 let llen = strlen(a:line) 42 let llen = strlen(a:line)
40 while i < llen 43 while i < llen
41 if a:line[i] == '"' 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
48 let i += 1
49 endwhile
50 if a:line[i] == '"'
51 let i += 1
52 endif
53 else
54 if a:line[i] == "'"
55 let i += 1
56 let line1 = line1 . 's'
57 while !(a:line[i] == "'" && ((i > 1 && a:line[i-1] == '\' && a:line[i-2] == '\') || a:line[i-1] != '\')) && i < llen
58 let i += 1
59 endwhile
60 if a:line[i] == "'"
61 let i += 1
62 endif
63 else
64 if a:line[i] == "`"
65 let i += 1
66 let line1 = line1 . 's'
67 while a:line[i] != "`" && i < llen
42 let i += 1 68 let i += 1
43 let line1 = line1 . 's' 69 endwhile
44 while !(a:line[i] == '"' && ((i > 1 && a:line[i-1] == '\' && a:line[i-2] == '\') || a:line[i-1] != '\')) && i < llen 70 if a:line[i] == "`"
45 let i += 1 71 let i += 1
46 endwhile 72 endif
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 endif
73 if i == llen 74 endif
74 break 75 endif
75 endif 76 if i == llen
76 let line1 = line1 . a:line[i] 77 break
77 let j += 1 78 endif
78 let i += 1 79 let line1 = line1 . a:line[i]
79 endwhile 80 let j += 1
80 return line1 81 let i += 1
82 endwhile
83 return line1
81 endfunction 84 endfunction
82 85
83 " Convert foo(bar()) int foo() 86 " Convert foo(bar()) int foo()
84 function s:RDelete_parens(line) 87 function s:RDelete_parens(line)
85 if s:Get_paren_balance(a:line, "(", ")") != 0 88 if s:Get_paren_balance(a:line, "(", ")") != 0
86 return a:line 89 return a:line
87 endif 90 endif
88 let i = 0 91 let i = 0
89 let j = 0 92 let j = 0
90 let line1 = "" 93 let line1 = ""
91 let llen = strlen(a:line) 94 let llen = strlen(a:line)
92 while i < llen 95 while i < llen
93 let line1 = line1 . a:line[i] 96 let line1 = line1 . a:line[i]
94 if a:line[i] == '(' 97 if a:line[i] == '('
95 let nop = 1 98 let nop = 1
96 while nop > 0 && i < llen 99 while nop > 0 && i < llen
97 let i += 1 100 let i += 1
98 if a:line[i] == ')' 101 if a:line[i] == ')'
99 let nop -= 1 102 let nop -= 1
100 else 103 else
101 if a:line[i] == '(' 104 if a:line[i] == '('
102 let nop += 1 105 let nop += 1
103 endif 106 endif
104 endif
105 endwhile
106 let line1 = line1 . a:line[i]
107 endif 107 endif
108 let i += 1 108 endwhile
109 endwhile 109 let line1 = line1 . a:line[i]
110 return line1 110 endif
111 let i += 1
112 endwhile
113 return line1
111 endfunction 114 endfunction
112 115
113 function! s:Get_paren_balance(line, o, c) 116 function! s:Get_paren_balance(line, o, c)
114 let line2 = substitute(a:line, a:o, "", "g") 117 let line2 = substitute(a:line, a:o, "", "g")
115 let openp = strlen(a:line) - strlen(line2) 118 let openp = strlen(a:line) - strlen(line2)
116 let line3 = substitute(line2, a:c, "", "g") 119 let line3 = substitute(line2, a:c, "", "g")
117 let closep = strlen(line2) - strlen(line3) 120 let closep = strlen(line2) - strlen(line3)
118 return openp - closep 121 return openp - closep
119 endfunction 122 endfunction
120 123
121 function! s:Get_matching_brace(linenr, o, c, delbrace) 124 function! s:Get_matching_brace(linenr, o, c, delbrace)
122 let line = SanitizeRLine(getline(a:linenr)) 125 let line = SanitizeRLine(getline(a:linenr))
123 if a:delbrace == 1 126 if a:delbrace == 1
124 let line = substitute(line, '{$', "", "") 127 let line = substitute(line, '{$', "", "")
125 endif 128 endif
126 let pb = s:Get_paren_balance(line, a:o, a:c) 129 let pb = s:Get_paren_balance(line, a:o, a:c)
127 let i = a:linenr 130 let i = a:linenr
128 while pb != 0 && i > 1 131 while pb != 0 && i > 1
129 let i -= 1 132 let i -= 1
130 let pb += s:Get_paren_balance(SanitizeRLine(getline(i)), a:o, a:c) 133 let pb += s:Get_paren_balance(SanitizeRLine(getline(i)), a:o, a:c)
131 endwhile 134 endwhile
132 return i 135 return i
133 endfunction 136 endfunction
134 137
135 " This function is buggy because there 'if's without 'else' 138 " This function is buggy because there 'if's without 'else'
136 " It must be rewritten relying more on indentation 139 " It must be rewritten relying more on indentation
137 function! s:Get_matching_if(linenr, delif) 140 function! s:Get_matching_if(linenr, delif)
138 " let filenm = expand("%") 141 let line = SanitizeRLine(getline(a:linenr))
139 " call writefile([filenm], "/tmp/matching_if_" . a:linenr) 142 if a:delif
140 let line = SanitizeRLine(getline(a:linenr)) 143 let line = substitute(line, "if", "", "g")
141 if a:delif 144 endif
142 let line = substitute(line, "if", "", "g") 145 let elsenr = 0
143 endif 146 let i = a:linenr
144 let elsenr = 0 147 let ifhere = 0
145 let i = a:linenr 148 while i > 0
146 let ifhere = 0 149 let line2 = substitute(line, '\<else\>', "xxx", "g")
147 while i > 0 150 let elsenr += strlen(line) - strlen(line2)
148 let line2 = substitute(line, '\<else\>', "xxx", "g") 151 if line =~ '.*\s*if\s*()' || line =~ '.*\s*if\s*()'
149 let elsenr += strlen(line) - strlen(line2) 152 let elsenr -= 1
150 if line =~ '.*\s*if\s*()' || line =~ '.*\s*if\s*()' 153 if elsenr == 0
151 let elsenr -= 1 154 let ifhere = i
152 if elsenr == 0 155 break
153 let ifhere = i 156 endif
154 break 157 endif
155 endif 158 let i -= 1
156 endif 159 let line = SanitizeRLine(getline(i))
157 let i -= 1 160 endwhile
158 let line = SanitizeRLine(getline(i)) 161 if ifhere
159 endwhile 162 return ifhere
160 if ifhere 163 else
161 return ifhere 164 return a:linenr
165 endif
166 endfunction
167
168 function! s:Get_last_paren_idx(line, o, c, pb)
169 let blc = a:pb
170 let line = substitute(a:line, '\t', s:curtabstop, "g")
171 let theidx = -1
172 let llen = strlen(line)
173 let idx = 0
174 while idx < llen
175 if line[idx] == a:o
176 let blc -= 1
177 if blc == 0
178 let theidx = idx
179 endif
162 else 180 else
163 return a:linenr 181 if line[idx] == a:c
164 endif 182 let blc += 1
165 endfunction 183 endif
166 184 endif
167 function! s:Get_last_paren_idx(line, o, c, pb) 185 let idx += 1
168 let blc = a:pb 186 endwhile
169 let line = substitute(a:line, '\t', s:curtabstop, "g") 187 return theidx + 1
170 let theidx = -1
171 let llen = strlen(line)
172 let idx = 0
173 while idx < llen
174 if line[idx] == a:o
175 let blc -= 1
176 if blc == 0
177 let theidx = idx
178 endif
179 else
180 if line[idx] == a:c
181 let blc += 1
182 endif
183 endif
184 let idx += 1
185 endwhile
186 return theidx + 1
187 endfunction 188 endfunction
188 189
189 " Get previous relevant line. Search back until getting a line that isn't 190 " Get previous relevant line. Search back until getting a line that isn't
190 " comment or blank 191 " comment or blank
191 function s:Get_prev_line(lineno) 192 function s:Get_prev_line(lineno)
192 let lnum = a:lineno - 1 193 let lnum = a:lineno - 1
194 let data = getline( lnum )
195 while lnum > 0 && (data =~ '^\s*#' || data =~ '^\s*$')
196 let lnum = lnum - 1
193 let data = getline( lnum ) 197 let data = getline( lnum )
194 while lnum > 0 && (data =~ '^\s*#' || data =~ '^\s*$') 198 endwhile
195 let lnum = lnum - 1 199 return lnum
196 let data = getline( lnum )
197 endwhile
198 return lnum
199 endfunction 200 endfunction
200 201
201 " This function is also used by r-plugin/common_global.vim 202 " This function is also used by r-plugin/common_global.vim
202 " Delete from '#' to the end of the line, unless the '#' is inside a string. 203 " Delete from '#' to the end of the line, unless the '#' is inside a string.
203 function SanitizeRLine(line) 204 function SanitizeRLine(line)
204 let newline = s:RDelete_quotes(a:line) 205 let newline = s:RDelete_quotes(a:line)
205 let newline = s:RDelete_parens(newline) 206 let newline = s:RDelete_parens(newline)
206 let newline = substitute(newline, '#.*', "", "") 207 let newline = substitute(newline, '#.*', "", "")
207 let newline = substitute(newline, '\s*$', "", "") 208 let newline = substitute(newline, '\s*$', "", "")
208 return newline 209 if &filetype == "rhelp" && newline =~ '^\\method{.*}{.*}(.*'
210 let newline = substitute(newline, '^\\method{\(.*\)}{.*}', '\1', "")
211 endif
212 return newline
209 endfunction 213 endfunction
210 214
211 function GetRIndent() 215 function GetRIndent()
212 216
213 let clnum = line(".") " current line 217 let clnum = line(".") " current line
214 218
215 let cline = getline(clnum) 219 let cline = getline(clnum)
216 if cline =~ '^\s*#' 220 if cline =~ '^\s*#'
217 if g:r_indent_ess_comments == 1 221 if g:r_indent_ess_comments == 1
218 if cline =~ '^\s*###' 222 if cline =~ '^\s*###'
219 return 0 223 return 0
224 endif
225 if cline !~ '^\s*##'
226 return g:r_indent_comment_column
227 endif
228 endif
229 endif
230
231 let cline = SanitizeRLine(cline)
232
233 if cline =~ '^\s*}' || cline =~ '^\s*}\s*)$'
234 let indline = s:Get_matching_brace(clnum, '{', '}', 1)
235 if indline > 0 && indline != clnum
236 let iline = SanitizeRLine(getline(indline))
237 if s:Get_paren_balance(iline, "(", ")") == 0 || iline =~ '(\s*{$'
238 return indent(indline)
239 else
240 let indline = s:Get_matching_brace(indline, '(', ')', 1)
241 return indent(indline)
242 endif
243 endif
244 endif
245
246 " 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
258 endif
259 if line =~ '^\\examples{' || line =~ '^\\usage{' || line =~ '^\\dontshow{' || line =~ '^\\dontrun{' || line =~ '^\\donttest{' || line =~ '^\\testonly{'
260 return 0
261 endif
262 endif
263
264 if &filetype == "rnoweb" && line =~ "^<<.*>>="
265 return 0
266 endif
267
268 if cline =~ '^\s*{'
269 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
276 if nline =~ '^\s*function\s*(' && indent(nlnum) == &sw
277 return 0
278 endif
279 endif
280 if s:Get_paren_balance(line, "(", ")") == 0
281 return indent(lnum)
282 endif
283 endif
284
285 " line is an incomplete command:
286 if line =~ '\<\(if\|while\|for\|function\)\s*()$' || line =~ '\<else$' || line =~ '<-$'
287 return indent(lnum) + &sw
288 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*{$'))
295 return indent(lnum) + &sw
296 endif
297
298 let s:curtabstop = repeat(' ', &tabstop)
299
300 if g:r_indent_align_args == 1
301 if pb > 0 && line =~ '{$'
302 return s:Get_last_paren_idx(line, '(', ')', pb) + &sw
303 endif
304
305 let bb = s:Get_paren_balance(line, '[', ']')
306
307 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
314 endif
315
316 if pb < 0 && line =~ '.*[,&|\-\*+<>]$'
317 let lnum = s:Get_prev_line(lnum)
318 while pb < 1 && lnum > 0
319 let line = SanitizeRLine(getline(lnum))
320 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
220 endif 328 endif
221 if cline !~ '^\s*##' 329 endif
222 return g:r_indent_comment_column 330 if pb == 1
223 endif 331 return ind + 1
224 endif 332 endif
225 endif 333 let ind -= 1
226 334 endwhile
227 let cline = SanitizeRLine(cline) 335 let lnum -= 1
228 336 endwhile
229 if cline =~ '^\s*}' || cline =~ '^\s*}\s*)$' 337 return 0
230 let indline = s:Get_matching_brace(clnum, '{', '}', 1) 338 endif
231 if indline > 0 && indline != clnum 339
232 let iline = SanitizeRLine(getline(indline)) 340 if bb > 0
233 if s:Get_paren_balance(iline, "(", ")") == 0 || iline =~ '(\s*{$' 341 let ind = s:Get_last_paren_idx(getline(lnum), '[', ']', bb)
234 return indent(indline) 342 return ind
235 else 343 endif
236 let indline = s:Get_matching_brace(indline, '(', ')', 1) 344 endif
237 return indent(indline) 345
238 endif 346 let post_block = 0
239 endif 347 if line =~ '}$'
240 endif 348 let lnum = s:Get_matching_brace(lnum, '{', '}', 0)
241
242 " Find the first non blank line above the current line
243 let lnum = s:Get_prev_line(clnum)
244 " Hit the start of the file, use zero indent.
245 if lnum == 0
246 return 0
247 endif
248
249 let line = SanitizeRLine(getline(lnum)) 349 let line = SanitizeRLine(getline(lnum))
250 350 if lnum > 0 && line =~ '^\s*{'
251 if &filetype == "rhelp" 351 let lnum = s:Get_prev_line(lnum)
252 if cline =~ '^\\dontshow{' || cline =~ '^\\dontrun{' || cline =~ '^\\donttest{' || cline =~ '^\\testonly{' 352 let line = SanitizeRLine(getline(lnum))
253 return 0 353 endif
254 endif 354 let pb = s:Get_paren_balance(line, '(', ')')
255 if line =~ '^\\examples{' || line =~ '^\\usage{' || line =~ '^\\dontshow{' || line =~ '^\\dontrun{' || line =~ '^\\donttest{' || line =~ '^\\testonly{' 355 let post_block = 1
256 return 0 356 endif
257 endif 357
258 if line =~ '^\\method{.*}{.*}(.*' 358 " Indent after operator pattern
259 let line = substitute(line, '^\\method{\(.*\)}{.*}', '\1', "") 359 let olnum = s:Get_prev_line(lnum)
260 endif 360 let oline = getline(olnum)
261 endif 361 if olnum > 0
262 362 if line =~ g:r_indent_op_pattern
363 if oline =~ g:r_indent_op_pattern
364 return indent(lnum)
365 else
366 return indent(lnum) + &sw
367 endif
368 else
369 if oline =~ g:r_indent_op_pattern
370 return indent(lnum) - &sw
371 endif
372 endif
373 endif
374
375 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
383 endwhile
384 if line =~ '{$' && post_block == 0
385 return indent(lnum) + &sw
386 endif
387
388 " Now we can do some tests again
263 if cline =~ '^\s*{' 389 if cline =~ '^\s*{'
264 if g:r_indent_ess_compatible && line =~ ')$' 390 return indent(lnum)
265 let nlnum = lnum 391 endif
266 let nline = line 392 if post_block == 0
267 while s:Get_paren_balance(nline, '(', ')') < 0 393 let newl = SanitizeRLine(line)
268 let nlnum = s:Get_prev_line(nlnum) 394 if newl =~ '\<\(if\|while\|for\|function\)\s*()$' || newl =~ '\<else$' || newl =~ '<-$'
269 let nline = SanitizeRLine(getline(nlnum)) . nline
270 endwhile
271 if nline =~ '^\s*function\s*(' && indent(nlnum) == &sw
272 return 0
273 endif
274 endif
275 if s:Get_paren_balance(line, "(", ")") == 0
276 return indent(lnum)
277 endif
278 endif
279
280 " line is an incomplete command:
281 if line =~ '\<\(if\|while\|for\|function\)\s*()$' || line =~ '\<else$' || line =~ '<-$'
282 return indent(lnum) + &sw 395 return indent(lnum) + &sw
283 endif 396 endif
284 397 endif
285 " Deal with () and [] 398 endif
286 399
287 let pb = s:Get_paren_balance(line, '(', ')') 400 if cline =~ '^\s*else'
288 401 if line =~ '<-\s*if\s*()'
289 if line =~ '^\s*{$' || line =~ '(\s*{' || (pb == 0 && (line =~ '{$' || line =~ '(\s*{$')) 402 return indent(lnum) + &sw
290 return indent(lnum) + &sw 403 else
291 endif 404 if line =~ '\<if\s*()'
292 405 return indent(lnum)
293 let bb = s:Get_paren_balance(line, '[', ']') 406 else
294 407 return indent(lnum) - &sw
295 let s:curtabstop = repeat(' ', &tabstop) 408 endif
296 if g:r_indent_align_args == 1 409 endif
297 410 endif
298 if pb == 0 && bb == 0 && (line =~ '.*[,&|\-\*+<>]$' || cline =~ '^\s*[,&|\-\*+<>]') 411
299 return indent(lnum) 412 let bb = s:Get_paren_balance(line, '[', ']')
300 endif 413 if bb < 0 && line =~ '.*]'
301 414 while bb < 0 && lnum > 0
302 if pb > 0 415 let lnum -= 1
303 if &filetype == "rhelp" 416 let linepiece = SanitizeRLine(getline(lnum))
304 let ind = s:Get_last_paren_idx(line, '(', ')', pb) 417 let bb += s:Get_paren_balance(linepiece, "[", "]")
305 else 418 let line = linepiece . line
306 let ind = s:Get_last_paren_idx(getline(lnum), '(', ')', pb) 419 endwhile
307 endif 420 let line = s:RDelete_parens(line)
308 return ind 421 endif
309 endif 422
310 423 let plnum = s:Get_prev_line(lnum)
311 if pb < 0 && line =~ '.*[,&|\-\*+<>]$' 424 let ppost_else = 0
312 let lnum = s:Get_prev_line(lnum) 425 if plnum > 0
313 while pb < 1 && lnum > 0 426 let pline = SanitizeRLine(getline(plnum))
314 let line = SanitizeRLine(getline(lnum)) 427 let ppost_block = 0
315 let line = substitute(line, '\t', s:curtabstop, "g") 428 if pline =~ '}$'
316 let ind = strlen(line) 429 let ppost_block = 1
317 while ind > 0 430 let plnum = s:Get_matching_brace(plnum, '{', '}', 0)
318 if line[ind] == ')' 431 let pline = SanitizeRLine(getline(plnum))
319 let pb -= 1 432 if pline =~ '^\s*{$' && plnum > 0
320 else 433 let plnum = s:Get_prev_line(plnum)
321 if line[ind] == '('
322 let pb += 1
323 endif
324 endif
325 if pb == 1
326 return ind + 1
327 endif
328 let ind -= 1
329 endwhile
330 let lnum -= 1
331 endwhile
332 return 0
333 endif
334
335 if bb > 0
336 let ind = s:Get_last_paren_idx(getline(lnum), '[', ']', bb)
337 return ind
338 endif
339 endif
340
341 let post_block = 0
342 if line =~ '}$'
343 let lnum = s:Get_matching_brace(lnum, '{', '}', 0)
344 let line = SanitizeRLine(getline(lnum))
345 if lnum > 0 && line =~ '^\s*{'
346 let lnum = s:Get_prev_line(lnum)
347 let line = SanitizeRLine(getline(lnum))
348 endif
349 let pb = s:Get_paren_balance(line, '(', ')')
350 let post_block = 1
351 endif
352
353 let post_fun = 0
354 if pb < 0 && line !~ ')\s*[,&|\-\*+<>]$'
355 let post_fun = 1
356 while pb < 0 && lnum > 0
357 let lnum -= 1
358 let linepiece = SanitizeRLine(getline(lnum))
359 let pb += s:Get_paren_balance(linepiece, "(", ")")
360 let line = linepiece . line
361 endwhile
362 if line =~ '{$' && post_block == 0
363 return indent(lnum) + &sw
364 endif
365
366 " Now we can do some tests again
367 if cline =~ '^\s*{'
368 return indent(lnum)
369 endif
370 if post_block == 0
371 let newl = SanitizeRLine(line)
372 if newl =~ '\<\(if\|while\|for\|function\)\s*()$' || newl =~ '\<else$' || newl =~ '<-$'
373 return indent(lnum) + &sw
374 endif
375 endif
376 endif
377
378 if cline =~ '^\s*else'
379 if line =~ '<-\s*if\s*()'
380 return indent(lnum) + &sw
381 else
382 if line =~ '\<if\s*()'
383 return indent(lnum)
384 else
385 return indent(lnum) - &sw
386 endif
387 endif
388 endif
389
390 if bb < 0 && line =~ '.*]'
391 while bb < 0 && lnum > 0
392 let lnum -= 1
393 let linepiece = SanitizeRLine(getline(lnum))
394 let bb += s:Get_paren_balance(linepiece, "[", "]")
395 let line = linepiece . line
396 endwhile
397 let line = s:RDelete_parens(line)
398 endif
399
400 let plnum = s:Get_prev_line(lnum)
401 let ppost_else = 0
402 if plnum > 0
403 let pline = SanitizeRLine(getline(plnum)) 434 let pline = SanitizeRLine(getline(plnum))
404 let ppost_block = 0 435 endif
405 if pline =~ '}$' 436 endif
406 let ppost_block = 1 437
407 let plnum = s:Get_matching_brace(plnum, '{', '}', 0) 438 if pline =~ 'else$'
408 let pline = SanitizeRLine(getline(plnum)) 439 let ppost_else = 1
409 if pline =~ '^\s*{$' && plnum > 0 440 let plnum = s:Get_matching_if(plnum, 0)
410 let plnum = s:Get_prev_line(plnum) 441 let pline = SanitizeRLine(getline(plnum))
411 let pline = SanitizeRLine(getline(plnum)) 442 endif
412 endif 443
413 endif 444 if pline =~ '^\s*else\s*if\s*('
414 445 let pplnum = s:Get_prev_line(plnum)
415 if pline =~ 'else$' 446 let ppline = SanitizeRLine(getline(pplnum))
416 let ppost_else = 1 447 while ppline =~ '^\s*else\s*if\s*(' || ppline =~ '^\s*if\s*()\s*\S$'
417 let plnum = s:Get_matching_if(plnum, 0) 448 let plnum = pplnum
418 let pline = SanitizeRLine(getline(plnum)) 449 let pline = ppline
419 endif 450 let pplnum = s:Get_prev_line(plnum)
420 451 let ppline = SanitizeRLine(getline(pplnum))
421 if pline =~ '^\s*else\s*if\s*(' 452 endwhile
422 let pplnum = s:Get_prev_line(plnum) 453 while ppline =~ '\<\(if\|while\|for\|function\)\s*()$' || ppline =~ '\<else$' || ppline =~ '<-$'
423 let ppline = SanitizeRLine(getline(pplnum)) 454 let plnum = pplnum
424 while ppline =~ '^\s*else\s*if\s*(' || ppline =~ '^\s*if\s*()\s*\S$' 455 let pline = ppline
425 let plnum = pplnum 456 let pplnum = s:Get_prev_line(plnum)
426 let pline = ppline 457 let ppline = SanitizeRLine(getline(pplnum))
427 let pplnum = s:Get_prev_line(plnum) 458 endwhile
428 let ppline = SanitizeRLine(getline(pplnum)) 459 endif
429 endwhile 460
430 while ppline =~ '\<\(if\|while\|for\|function\)\s*()$' || ppline =~ '\<else$' || ppline =~ '<-$' 461 let ppb = s:Get_paren_balance(pline, '(', ')')
431 let plnum = pplnum 462 if ppb < 0 && (pline =~ ')\s*{$' || pline =~ ')$')
432 let pline = ppline 463 while ppb < 0 && plnum > 0
433 let pplnum = s:Get_prev_line(plnum) 464 let plnum -= 1
434 let ppline = SanitizeRLine(getline(pplnum)) 465 let linepiece = SanitizeRLine(getline(plnum))
435 endwhile 466 let ppb += s:Get_paren_balance(linepiece, "(", ")")
436 endif 467 let pline = linepiece . pline
437 468 endwhile
438 let ppb = s:Get_paren_balance(pline, '(', ')') 469 let pline = s:RDelete_parens(pline)
439 if ppb < 0 && (pline =~ ')\s*{$' || pline =~ ')$') 470 endif
440 while ppb < 0 && plnum > 0 471 endif
441 let plnum -= 1 472
442 let linepiece = SanitizeRLine(getline(plnum)) 473 let ind = indent(lnum)
443 let ppb += s:Get_paren_balance(linepiece, "(", ")") 474 let pind = indent(plnum)
444 let pline = linepiece . pline 475
445 endwhile 476 if g:r_indent_align_args == 0 && pb != 0
446 let pline = s:RDelete_parens(pline) 477 let ind += pb * &sw
447 endif 478 return ind
448 endif 479 endif
449 480
450 let ind = indent(lnum) 481 if g:r_indent_align_args == 0 && bb != 0
482 let ind += bb * &sw
483 return ind
484 endif
485
486 if ind == pind || (ind == (pind + &sw) && pline =~ '{$' && ppost_else == 0)
487 return ind
488 endif
489
490 let pline = getline(plnum)
491 let pbb = s:Get_paren_balance(pline, '[', ']')
492
493 while pind < ind && plnum > 0 && ppb == 0 && pbb == 0
494 let ind = pind
495 let plnum = s:Get_prev_line(plnum)
496 let pline = getline(plnum)
497 let ppb = s:Get_paren_balance(pline, '(', ')')
498 let pbb = s:Get_paren_balance(pline, '[', ']')
499 while pline =~ '^\s*else'
500 let plnum = s:Get_matching_if(plnum, 1)
501 let pline = getline(plnum)
502 let ppb = s:Get_paren_balance(pline, '(', ')')
503 let pbb = s:Get_paren_balance(pline, '[', ']')
504 endwhile
451 let pind = indent(plnum) 505 let pind = indent(plnum)
452 506 if ind == (pind + &sw) && pline =~ '{$'
453 if g:r_indent_align_args == 0 && pb != 0 507 return ind
454 let ind += pb * &sw 508 endif
455 return ind 509 endwhile
456 endif 510
457 511 return ind
458 if g:r_indent_align_args == 0 && bb != 0 512
459 let ind += bb * &sw 513 endfunction
460 return ind 514
461 endif 515 " vim: sw=2
462
463 if ind == pind || (ind == (pind + &sw) && pline =~ '{$' && ppost_else == 0)
464 return ind
465 endif
466
467 let pline = getline(plnum)
468 let pbb = s:Get_paren_balance(pline, '[', ']')
469
470 while pind < ind && plnum > 0 && ppb == 0 && pbb == 0
471 let ind = pind
472 let plnum = s:Get_prev_line(plnum)
473 let pline = getline(plnum)
474 let ppb = s:Get_paren_balance(pline, '(', ')')
475 let pbb = s:Get_paren_balance(pline, '[', ']')
476 while pline =~ '^\s*else'
477 let plnum = s:Get_matching_if(plnum, 1)
478 let pline = getline(plnum)
479 let ppb = s:Get_paren_balance(pline, '(', ')')
480 let pbb = s:Get_paren_balance(pline, '[', ']')
481 endwhile
482 let pind = indent(plnum)
483 if ind == (pind + &sw) && pline =~ '{$'
484 return ind
485 endif
486 endwhile
487
488 return ind
489
490 endfunction
491
492 " vim: sw=4