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