7
|
1 " Vim indent file
|
|
2 " Language: PHP
|
409
|
3 " Author: John Wellesz <John.wellesz (AT) teaser (DOT) fr>
|
|
4 " URL: http://www.2072productions.com/vim/indent/php.vim
|
2034
|
5 " Last Change: 2008 November 22nd
|
631
|
6 " Newsletter: http://www.2072productions.com/?to=php-indent-for-vim-newsletter.php
|
2034
|
7 " Version: 1.30
|
557
|
8 "
|
856
|
9 "
|
557
|
10 " If you find a bug, please e-mail me at John.wellesz (AT) teaser (DOT) fr
|
631
|
11 " with an example of code that breaks the algorithm.
|
409
|
12 "
|
|
13 "
|
557
|
14 " Thanks a lot for using this script.
|
409
|
15 "
|
|
16 "
|
|
17 " NOTE: This script must be used with PHP syntax ON and with the php syntax
|
1668
|
18 " script by Lutz Eymers (http://www.isp.de/data/php.vim ) or with the
|
|
19 " script by Peter Hodge (http://www.vim.org/scripts/script.php?script_id=1571 )
|
2034
|
20 " the later is bunbdled by default with Vim 7.
|
409
|
21 "
|
|
22 "
|
1668
|
23 " In the case you have syntax errors in your script such as HereDoc end
|
|
24 " identifiers not at col 1 you'll have to indent your file 2 times (This
|
|
25 " script will automatically put HereDoc end identifiers at col 1 if
|
|
26 " they are followed by a ';').
|
856
|
27 "
|
557
|
28 "
|
2034
|
29 " NOTE: If you are editing files in Unix file format and that (by accident)
|
|
30 " there are '\r' before new lines, this script won't be able to proceed
|
|
31 " correctly and will make many mistakes because it won't be able to match
|
|
32 " '\s*$' correctly.
|
|
33 " So you have to remove those useless characters first with a command like:
|
409
|
34 "
|
2034
|
35 " :%s /\r$//g
|
7
|
36 "
|
2034
|
37 " or simply 'let' the option PHP_removeCRwhenUnix to 1 and the script will
|
|
38 " silently remove them when VIM load this script (at each bufread).
|
409
|
39 "
|
2034
|
40 " Options: See :help php-indent for available options.
|
|
41
|
557
|
42
|
7
|
43 if exists("b:did_indent")
|
532
|
44 finish
|
7
|
45 endif
|
|
46 let b:did_indent = 1
|
|
47
|
409
|
48
|
|
49 let php_sync_method = 0
|
|
50
|
|
51
|
|
52 if exists("PHP_default_indenting")
|
532
|
53 let b:PHP_default_indenting = PHP_default_indenting * &sw
|
409
|
54 else
|
532
|
55 let b:PHP_default_indenting = 0
|
409
|
56 endif
|
|
57
|
|
58 if exists("PHP_BracesAtCodeLevel")
|
532
|
59 let b:PHP_BracesAtCodeLevel = PHP_BracesAtCodeLevel
|
409
|
60 else
|
532
|
61 let b:PHP_BracesAtCodeLevel = 0
|
409
|
62 endif
|
|
63
|
1668
|
64
|
557
|
65 if exists("PHP_autoformatcomment")
|
|
66 let b:PHP_autoformatcomment = PHP_autoformatcomment
|
|
67 else
|
|
68 let b:PHP_autoformatcomment = 1
|
|
69 endif
|
7
|
70
|
1668
|
71 if exists("PHP_vintage_case_default_indent")
|
|
72 let b:PHP_vintage_case_default_indent = PHP_vintage_case_default_indent
|
|
73 else
|
|
74 let b:PHP_vintage_case_default_indent = 0
|
|
75 endif
|
|
76
|
|
77
|
|
78
|
409
|
79 let b:PHP_lastindented = 0
|
|
80 let b:PHP_indentbeforelast = 0
|
|
81 let b:PHP_indentinghuge = 0
|
|
82 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting
|
|
83 let b:PHP_LastIndentedWasComment = 0
|
|
84 let b:PHP_InsideMultilineComment = 0
|
|
85 let b:InPHPcode = 0
|
|
86 let b:InPHPcode_checked = 0
|
|
87 let b:InPHPcode_and_script = 0
|
|
88 let b:InPHPcode_tofind = ""
|
|
89 let b:PHP_oldchangetick = b:changedtick
|
|
90 let b:UserIsTypingComment = 0
|
|
91 let b:optionsset = 0
|
|
92
|
|
93 setlocal nosmartindent
|
856
|
94 setlocal noautoindent
|
409
|
95 setlocal nocindent
|
532
|
96 setlocal nolisp
|
409
|
97
|
|
98 setlocal indentexpr=GetPhpIndent()
|
|
99 setlocal indentkeys=0{,0},0),:,!^F,o,O,e,*<Return>,=?>,=<?,=*/
|
|
100
|
|
101
|
532
|
102
|
|
103 let s:searchpairflags = 'bWr'
|
409
|
104
|
|
105 if &fileformat == "unix" && exists("PHP_removeCRwhenUnix") && PHP_removeCRwhenUnix
|
532
|
106 silent! %s/\r$//g
|
409
|
107 endif
|
|
108
|
7
|
109 if exists("*GetPhpIndent")
|
2034
|
110 call ResetPhpOptions()
|
631
|
111 finish " XXX
|
7
|
112 endif
|
|
113
|
409
|
114 let s:endline= '\s*\%(//.*\|#.*\|/\*.*\*/\s*\)\=$'
|
|
115 let s:PHP_startindenttag = '<?\%(.*?>\)\@!\|<script[^>]*>\%(.*<\/script>\)\@!'
|
1120
|
116 "setlocal debug=msg " XXX
|
409
|
117
|
|
118
|
|
119 function! GetLastRealCodeLNum(startline) " {{{
|
856
|
120
|
532
|
121 let lnum = a:startline
|
856
|
122
|
631
|
123 if b:GetLastRealCodeLNum_ADD && b:GetLastRealCodeLNum_ADD == lnum + 1
|
|
124 let lnum = b:GetLastRealCodeLNum_ADD
|
|
125 endif
|
856
|
126
|
532
|
127 let old_lnum = lnum
|
409
|
128
|
532
|
129 while lnum > 1
|
|
130 let lnum = prevnonblank(lnum)
|
|
131 let lastline = getline(lnum)
|
409
|
132
|
532
|
133 if b:InPHPcode_and_script && lastline =~ '?>\s*$'
|
|
134 let lnum = lnum - 1
|
|
135 elseif lastline =~ '^\s*?>.*<?\%(php\)\=\s*$'
|
|
136 let lnum = lnum - 1
|
|
137 elseif lastline =~ '^\s*\%(//\|#\|/\*.*\*/\s*$\)'
|
|
138 let lnum = lnum - 1
|
|
139 elseif lastline =~ '\*/\s*$'
|
|
140 call cursor(lnum, 1)
|
|
141 if lastline !~ '^\*/'
|
|
142 call search('\*/', 'W')
|
|
143 endif
|
557
|
144 let lnum = searchpair('/\*', '', '\*/', s:searchpairflags, 'Skippmatch2()')
|
409
|
145
|
532
|
146 let lastline = getline(lnum)
|
|
147 if lastline =~ '^\s*/\*'
|
|
148 let lnum = lnum - 1
|
|
149 else
|
|
150 break
|
|
151 endif
|
409
|
152
|
|
153
|
532
|
154 elseif lastline =~? '\%(//\s*\|?>.*\)\@<!<?\%(php\)\=\s*$\|^\s*<script\>'
|
|
155
|
|
156 while lastline !~ '\(<?.*\)\@<!?>' && lnum > 1
|
|
157 let lnum = lnum - 1
|
|
158 let lastline = getline(lnum)
|
|
159 endwhile
|
|
160 if lastline =~ '^\s*?>'
|
|
161 let lnum = lnum - 1
|
|
162 else
|
|
163 break
|
|
164 endif
|
|
165
|
409
|
166
|
1668
|
167 elseif lastline =~? '^\a\w*;\=$' && lastline !~? s:notPhpHereDoc " XXX 0607
|
|
168 let tofind=substitute( lastline, '\(\a\w*\);\=', '<<<''\\=\1''\\=$', '') " XXX 0607
|
532
|
169 while getline(lnum) !~? tofind && lnum > 1
|
|
170 let lnum = lnum - 1
|
|
171 endwhile
|
|
172 else
|
856
|
173 break
|
409
|
174 endif
|
532
|
175 endwhile
|
|
176
|
|
177 if lnum==1 && getline(lnum)!~ '<?'
|
|
178 let lnum=0
|
|
179 endif
|
|
180
|
|
181 if b:InPHPcode_and_script && !b:InPHPcode
|
|
182 let b:InPHPcode_and_script = 0
|
|
183 endif
|
1120
|
184
|
|
185
|
|
186
|
532
|
187 return lnum
|
|
188 endfunction " }}}
|
409
|
189
|
557
|
190 function! Skippmatch2()
|
|
191
|
|
192 let line = getline(".")
|
|
193
|
1668
|
194 if line =~ '\%(".*\)\@<=/\*\%(.*"\)\@=' || line =~ '\%(\%(//\|#\).*\)\@<=/\*'
|
557
|
195 return 1
|
|
196 else
|
|
197 return 0
|
|
198 endif
|
|
199 endfun
|
|
200
|
409
|
201 function! Skippmatch() " {{{
|
532
|
202 let synname = synIDattr(synID(line("."), col("."), 0), "name")
|
1668
|
203 if synname == "Delimiter" || synname == "phpRegionDelimiter" || synname =~# "^phpParent" || synname == "phpArrayParens" || synname =~# '^php\%(Block\|Brace\)' || synname == "javaScriptBraces" || synname =~# "^phpComment" && b:UserIsTypingComment
|
532
|
204 return 0
|
|
205 else
|
|
206 return 1
|
|
207 endif
|
|
208 endfun " }}}
|
409
|
209
|
|
210 function! FindOpenBracket(lnum) " {{{
|
532
|
211 call cursor(a:lnum, 1)
|
|
212 return searchpair('{', '', '}', 'bW', 'Skippmatch()')
|
|
213 endfun " }}}
|
409
|
214
|
|
215 function! FindTheIfOfAnElse (lnum, StopAfterFirstPrevElse) " {{{
|
532
|
216
|
|
217 if getline(a:lnum) =~# '^\s*}\s*else\%(if\)\=\>'
|
|
218 let beforeelse = a:lnum
|
|
219 else
|
|
220 let beforeelse = GetLastRealCodeLNum(a:lnum - 1)
|
|
221 endif
|
409
|
222
|
532
|
223 if !s:level
|
|
224 let s:iftoskip = 0
|
|
225 endif
|
|
226
|
|
227 if getline(beforeelse) =~# '^\s*\%(}\s*\)\=else\%(\s*if\)\@!\>'
|
|
228 let s:iftoskip = s:iftoskip + 1
|
|
229 endif
|
|
230
|
|
231 if getline(beforeelse) =~ '^\s*}'
|
|
232 let beforeelse = FindOpenBracket(beforeelse)
|
|
233
|
|
234 if getline(beforeelse) =~ '^\s*{'
|
|
235 let beforeelse = GetLastRealCodeLNum(beforeelse - 1)
|
409
|
236 endif
|
532
|
237 endif
|
409
|
238
|
|
239
|
532
|
240 if !s:iftoskip && a:StopAfterFirstPrevElse && getline(beforeelse) =~# '^\s*\%([}]\s*\)\=else\%(if\)\=\>'
|
|
241 return beforeelse
|
|
242 endif
|
|
243
|
|
244 if getline(beforeelse) !~# '^\s*if\>' && beforeelse>1 || s:iftoskip && beforeelse>1
|
|
245
|
|
246 if s:iftoskip && getline(beforeelse) =~# '^\s*if\>'
|
|
247 let s:iftoskip = s:iftoskip - 1
|
409
|
248 endif
|
|
249
|
532
|
250 let s:level = s:level + 1
|
|
251 let beforeelse = FindTheIfOfAnElse(beforeelse, a:StopAfterFirstPrevElse)
|
|
252 endif
|
409
|
253
|
532
|
254 return beforeelse
|
409
|
255
|
532
|
256 endfunction " }}}
|
409
|
257
|
|
258 function! IslinePHP (lnum, tofind) " {{{
|
532
|
259 let cline = getline(a:lnum)
|
409
|
260
|
532
|
261 if a:tofind==""
|
557
|
262 let tofind = "^\\s*[\"']*\\s*\\zs\\S"
|
532
|
263 else
|
|
264 let tofind = a:tofind
|
|
265 endif
|
409
|
266
|
532
|
267 let tofind = tofind . '\c'
|
409
|
268
|
532
|
269 let coltotest = match (cline, tofind) + 1
|
|
270
|
|
271 let synname = synIDattr(synID(a:lnum, coltotest, 0), "name")
|
409
|
272
|
532
|
273 if synname =~ '^php' || synname=="Delimiter" || synname =~? '^javaScript'
|
|
274 return synname
|
|
275 else
|
|
276 return ""
|
|
277 endif
|
|
278 endfunction " }}}
|
409
|
279
|
1668
|
280 let s:notPhpHereDoc = '\%(break\|return\|continue\|exit\|else\)'
|
|
281 let s:blockstart = '\%(\%(\%(}\s*\)\=else\%(\s\+\)\=\)\=if\>\|else\>\|while\>\|switch\>\|for\%(each\)\=\>\|declare\>\|class\>\|interface\>\|abstract\>\|try\>\|catch\>\)'
|
409
|
282
|
2034
|
283 let s:autoresetoptions = 0
|
|
284 if ! s:autoresetoptions
|
|
285 let s:autoresetoptions = 1
|
7
|
286 endif
|
|
287
|
2034
|
288 function! ResetPhpOptions()
|
|
289 if ! b:optionsset && &filetype == "php"
|
557
|
290 if b:PHP_autoformatcomment
|
|
291
|
|
292 setlocal comments=s1:/*,mb:*,ex:*/,://,:#
|
856
|
293
|
1668
|
294 setlocal formatoptions-=t
|
557
|
295 setlocal formatoptions+=q
|
|
296 setlocal formatoptions+=r
|
|
297 setlocal formatoptions+=o
|
1668
|
298 setlocal formatoptions+=w
|
557
|
299 setlocal formatoptions+=c
|
|
300 setlocal formatoptions+=b
|
|
301 endif
|
532
|
302 let b:optionsset = 1
|
|
303 endif
|
409
|
304 endfunc
|
|
305
|
2034
|
306 call ResetPhpOptions()
|
|
307
|
409
|
308 function! GetPhpIndent()
|
|
309
|
631
|
310 let b:GetLastRealCodeLNum_ADD = 0
|
|
311
|
532
|
312 let UserIsEditing=0
|
|
313 if b:PHP_oldchangetick != b:changedtick
|
|
314 let b:PHP_oldchangetick = b:changedtick
|
|
315 let UserIsEditing=1
|
|
316 endif
|
409
|
317
|
532
|
318 if b:PHP_default_indenting
|
|
319 let b:PHP_default_indenting = g:PHP_default_indenting * &sw
|
|
320 endif
|
|
321
|
|
322 let cline = getline(v:lnum)
|
409
|
323
|
856
|
324 if !b:PHP_indentinghuge && b:PHP_lastindented > b:PHP_indentbeforelast
|
532
|
325 if b:PHP_indentbeforelast
|
|
326 let b:PHP_indentinghuge = 1
|
2034
|
327 echom 'Large indenting detected, speed optimizations engaged (v1.30)'
|
532
|
328 endif
|
|
329 let b:PHP_indentbeforelast = b:PHP_lastindented
|
|
330 endif
|
409
|
331
|
532
|
332 if b:InPHPcode_checked && prevnonblank(v:lnum - 1) != b:PHP_lastindented
|
|
333 if b:PHP_indentinghuge
|
|
334 echom 'Large indenting deactivated'
|
|
335 let b:PHP_indentinghuge = 0
|
|
336 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting
|
409
|
337 endif
|
532
|
338 let b:PHP_lastindented = v:lnum
|
|
339 let b:PHP_LastIndentedWasComment=0
|
|
340 let b:PHP_InsideMultilineComment=0
|
|
341 let b:PHP_indentbeforelast = 0
|
409
|
342
|
532
|
343 let b:InPHPcode = 0
|
|
344 let b:InPHPcode_checked = 0
|
|
345 let b:InPHPcode_and_script = 0
|
|
346 let b:InPHPcode_tofind = ""
|
409
|
347
|
532
|
348 elseif v:lnum > b:PHP_lastindented
|
|
349 let real_PHP_lastindented = b:PHP_lastindented
|
|
350 let b:PHP_lastindented = v:lnum
|
|
351 endif
|
409
|
352
|
|
353
|
532
|
354 if !b:InPHPcode_checked " {{{ One time check
|
|
355 let b:InPHPcode_checked = 1
|
409
|
356
|
631
|
357 let synname = ""
|
|
358 if cline !~ '<?.*?>'
|
|
359 let synname = IslinePHP (prevnonblank(v:lnum), "")
|
|
360 endif
|
409
|
361
|
532
|
362 if synname!=""
|
1120
|
363 if synname != "phpHereDoc" && synname != "phpHereDocDelimiter"
|
532
|
364 let b:InPHPcode = 1
|
|
365 let b:InPHPcode_tofind = ""
|
409
|
366
|
1668
|
367 if synname =~# "^phpComment"
|
532
|
368 let b:UserIsTypingComment = 1
|
|
369 else
|
|
370 let b:UserIsTypingComment = 0
|
409
|
371 endif
|
|
372
|
532
|
373 if synname =~? '^javaScript'
|
|
374 let b:InPHPcode_and_script = 1
|
|
375 endif
|
409
|
376
|
532
|
377 else
|
|
378 let b:InPHPcode = 0
|
|
379 let b:UserIsTypingComment = 0
|
409
|
380
|
532
|
381 let lnum = v:lnum - 1
|
1668
|
382 while getline(lnum) !~? '<<<''\=\a\w*''\=$' && lnum > 1
|
532
|
383 let lnum = lnum - 1
|
|
384 endwhile
|
409
|
385
|
1668
|
386 let b:InPHPcode_tofind = substitute( getline(lnum), '^.*<<<''\=\(\a\w*\)''\=$', '^\\s*\1;\\=$', '') " XXX 0607
|
532
|
387 endif
|
|
388 else
|
|
389 let b:InPHPcode = 0
|
|
390 let b:UserIsTypingComment = 0
|
|
391 let b:InPHPcode_tofind = '<?\%(.*?>\)\@!\|<script.*>'
|
409
|
392 endif
|
532
|
393 endif "!b:InPHPcode_checked }}}
|
409
|
394
|
|
395
|
532
|
396 " Test if we are indenting PHP code {{{
|
|
397 let lnum = prevnonblank(v:lnum - 1)
|
|
398 let last_line = getline(lnum)
|
409
|
399
|
532
|
400 if b:InPHPcode_tofind!=""
|
|
401 if cline =~? b:InPHPcode_tofind
|
|
402 let b:InPHPcode = 1
|
|
403 let b:InPHPcode_tofind = ""
|
|
404 let b:UserIsTypingComment = 0
|
|
405 if cline =~ '\*/'
|
409
|
406 call cursor(v:lnum, 1)
|
532
|
407 if cline !~ '^\*/'
|
|
408 call search('\*/', 'W')
|
|
409 endif
|
557
|
410 let lnum = searchpair('/\*', '', '\*/', s:searchpairflags, 'Skippmatch2()')
|
409
|
411
|
|
412 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting
|
|
413
|
532
|
414 let b:PHP_LastIndentedWasComment = 0
|
409
|
415
|
532
|
416 if cline =~ '^\s*\*/'
|
|
417 return indent(lnum) + 1
|
|
418 else
|
|
419 return indent(lnum)
|
7
|
420 endif
|
532
|
421
|
|
422 elseif cline =~? '<script\>'
|
|
423 let b:InPHPcode_and_script = 1
|
631
|
424 let b:GetLastRealCodeLNum_ADD = v:lnum
|
532
|
425 endif
|
7
|
426 endif
|
532
|
427 endif
|
409
|
428
|
532
|
429 if b:InPHPcode
|
409
|
430
|
1120
|
431 if !b:InPHPcode_and_script && last_line =~ '\%(<?.*\)\@<!?>\%(.*<?\)\@!' && IslinePHP(lnum, '?>')=~"Delimiter"
|
532
|
432 if cline !~? s:PHP_startindenttag
|
|
433 let b:InPHPcode = 0
|
|
434 let b:InPHPcode_tofind = s:PHP_startindenttag
|
|
435 elseif cline =~? '<script\>'
|
|
436 let b:InPHPcode_and_script = 1
|
|
437 endif
|
409
|
438
|
1668
|
439 elseif last_line =~? '<<<''\=\a\w*''\=$' " XXX 0607
|
532
|
440 let b:InPHPcode = 0
|
1668
|
441 let b:InPHPcode_tofind = substitute( last_line, '^.*<<<''\=\(\a\w*\)''\=$', '^\\s*\1;\\=$', '') " XXX 0607
|
532
|
442
|
|
443 elseif !UserIsEditing && cline =~ '^\s*/\*\%(.*\*/\)\@!' && getline(v:lnum + 1) !~ '^\s*\*'
|
|
444 let b:InPHPcode = 0
|
|
445 let b:InPHPcode_tofind = '\*/'
|
409
|
446
|
532
|
447 elseif cline =~? '^\s*</script>'
|
|
448 let b:InPHPcode = 0
|
|
449 let b:InPHPcode_tofind = s:PHP_startindenttag
|
|
450 endif
|
|
451 endif " }}}
|
|
452
|
856
|
453
|
532
|
454 if !b:InPHPcode && !b:InPHPcode_and_script
|
|
455 return -1
|
|
456 endif
|
409
|
457
|
532
|
458 " Indent successive // or # comment the same way the first is {{{
|
|
459 if cline =~ '^\s*\%(//\|#\|/\*.*\*/\s*$\)'
|
|
460 if b:PHP_LastIndentedWasComment == 1
|
|
461 return indent(real_PHP_lastindented)
|
|
462 endif
|
|
463 let b:PHP_LastIndentedWasComment = 1
|
|
464 else
|
|
465 let b:PHP_LastIndentedWasComment = 0
|
|
466 endif " }}}
|
|
467
|
|
468 " Indent multiline /* comments correctly {{{
|
|
469
|
|
470 if b:PHP_InsideMultilineComment || b:UserIsTypingComment
|
|
471 if cline =~ '^\s*\*\%(\/\)\@!'
|
|
472 if last_line =~ '^\s*/\*'
|
|
473 return indent(lnum) + 1
|
|
474 else
|
|
475 return indent(lnum)
|
|
476 endif
|
|
477 else
|
|
478 let b:PHP_InsideMultilineComment = 0
|
|
479 endif
|
|
480 endif
|
|
481
|
631
|
482 if !b:PHP_InsideMultilineComment && cline =~ '^\s*/\*' && cline !~ '\*/\s*$'
|
|
483 if getline(v:lnum + 1) !~ '^\s*\*'
|
|
484 return -1
|
|
485 endif
|
532
|
486 let b:PHP_InsideMultilineComment = 1
|
|
487 endif " }}}
|
409
|
488
|
|
489
|
532
|
490 " Things always indented at col 1 (PHP delimiter: <?, ?>, Heredoc end) {{{
|
|
491 if cline =~# '^\s*<?' && cline !~ '?>'
|
|
492 return 0
|
|
493 endif
|
409
|
494
|
856
|
495 if cline =~ '^\s*?>' && cline !~# '<?'
|
532
|
496 return 0
|
|
497 endif
|
409
|
498
|
1668
|
499 if cline =~? '^\s*\a\w*;$\|^\a\w*$' && cline !~? s:notPhpHereDoc " XXX 0607
|
532
|
500 return 0
|
|
501 endif " }}}
|
409
|
502
|
532
|
503 let s:level = 0
|
409
|
504
|
532
|
505 let lnum = GetLastRealCodeLNum(v:lnum - 1)
|
631
|
506
|
532
|
507 let last_line = getline(lnum)
|
|
508 let ind = indent(lnum)
|
|
509 let endline= s:endline
|
409
|
510
|
532
|
511 if ind==0 && b:PHP_default_indenting
|
|
512 let ind = b:PHP_default_indenting
|
|
513 endif
|
409
|
514
|
532
|
515 if lnum == 0
|
|
516 return b:PHP_default_indenting
|
|
517 endif
|
409
|
518
|
|
519
|
532
|
520 if cline =~ '^\s*}\%(}}\)\@!'
|
|
521 let ind = indent(FindOpenBracket(v:lnum))
|
|
522 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting
|
|
523 return ind
|
|
524 endif
|
409
|
525
|
532
|
526 if cline =~ '^\s*\*/'
|
|
527 call cursor(v:lnum, 1)
|
|
528 if cline !~ '^\*/'
|
|
529 call search('\*/', 'W')
|
|
530 endif
|
557
|
531 let lnum = searchpair('/\*', '', '\*/', s:searchpairflags, 'Skippmatch2()')
|
532
|
532
|
|
533 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting
|
409
|
534
|
532
|
535 if cline =~ '^\s*\*/'
|
|
536 return indent(lnum) + 1
|
|
537 else
|
|
538 return indent(lnum)
|
|
539 endif
|
|
540 endif
|
|
541
|
|
542 let defaultORcase = '^\s*\%(default\|case\).*:'
|
|
543
|
1668
|
544 if last_line =~ '[;}]'.endline && last_line !~ '^)' && last_line !~# defaultORcase " Added && last_line !~ '^)' on 2007-12-30
|
532
|
545 if ind==b:PHP_default_indenting
|
|
546 return b:PHP_default_indenting
|
|
547 elseif b:PHP_indentinghuge && ind==b:PHP_CurrentIndentLevel && cline !~# '^\s*\%(else\|\%(case\|default\).*:\|[})];\=\)' && last_line !~# '^\s*\%(\%(}\s*\)\=else\)' && getline(GetLastRealCodeLNum(lnum - 1))=~';'.endline
|
|
548 return b:PHP_CurrentIndentLevel
|
|
549 endif
|
|
550 endif
|
|
551
|
|
552 let LastLineClosed = 0
|
|
553
|
1668
|
554 let terminated = '\%(;\%(\s*?>\)\=\|<<<''\=\a\w*''\=$\|^\s*}\)'.endline " XXX 0607
|
532
|
555
|
|
556 let unstated = '\%(^\s*'.s:blockstart.'.*)\|\%(//.*\)\@<!\<e'.'lse\>\)'.endline
|
409
|
557
|
532
|
558 if ind != b:PHP_default_indenting && cline =~# '^\s*else\%(if\)\=\>'
|
|
559 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting
|
|
560 return indent(FindTheIfOfAnElse(v:lnum, 1))
|
1668
|
561 elseif cline =~ '^\s*)\=\s*{'
|
532
|
562 let previous_line = last_line
|
|
563 let last_line_num = lnum
|
|
564
|
|
565 while last_line_num > 1
|
|
566
|
1668
|
567 if previous_line =~ '^\s*\%(' . s:blockstart . '\|\%([a-zA-Z]\s*\)*function\)'
|
532
|
568
|
|
569 let ind = indent(last_line_num)
|
|
570
|
|
571 if b:PHP_BracesAtCodeLevel
|
|
572 let ind = ind + &sw
|
|
573 endif
|
|
574
|
856
|
575 return ind
|
532
|
576 endif
|
|
577
|
|
578 let last_line_num = last_line_num - 1
|
|
579 let previous_line = getline(last_line_num)
|
|
580 endwhile
|
409
|
581
|
1668
|
582 elseif last_line =~# unstated && cline !~ '^\s*);\='.endline
|
|
583 let ind = ind + &sw " we indent one level further when the preceding line is not stated
|
532
|
584 return ind
|
|
585
|
1668
|
586 elseif (ind != b:PHP_default_indenting || last_line =~ '^)' ) && last_line =~ terminated " Added || last_line =~ '^)' on 2007-12-30 (array indenting [rpblem broke other things)
|
532
|
587 let previous_line = last_line
|
|
588 let last_line_num = lnum
|
|
589 let LastLineClosed = 1
|
|
590
|
|
591 while 1
|
|
592 if previous_line =~ '^\s*}'
|
|
593 let last_line_num = FindOpenBracket(last_line_num)
|
|
594
|
|
595 if getline(last_line_num) =~ '^\s*{'
|
|
596 let last_line_num = GetLastRealCodeLNum(last_line_num - 1)
|
409
|
597 endif
|
532
|
598
|
|
599 let previous_line = getline(last_line_num)
|
7
|
600
|
532
|
601 continue
|
|
602 else
|
409
|
603
|
532
|
604 if getline(last_line_num) =~# '^\s*else\%(if\)\=\>'
|
|
605 let last_line_num = FindTheIfOfAnElse(last_line_num, 0)
|
|
606 continue
|
|
607 endif
|
409
|
608
|
|
609
|
532
|
610 let last_match = last_line_num
|
409
|
611
|
532
|
612 let one_ahead_indent = indent(last_line_num)
|
|
613 let last_line_num = GetLastRealCodeLNum(last_line_num - 1)
|
|
614 let two_ahead_indent = indent(last_line_num)
|
|
615 let after_previous_line = previous_line
|
|
616 let previous_line = getline(last_line_num)
|
409
|
617
|
|
618
|
532
|
619 if previous_line =~# defaultORcase.'\|{'.endline
|
|
620 break
|
|
621 endif
|
409
|
622
|
532
|
623 if after_previous_line=~# '^\s*'.s:blockstart.'.*)'.endline && previous_line =~# '[;}]'.endline
|
|
624 break
|
7
|
625 endif
|
409
|
626
|
856
|
627 if one_ahead_indent == two_ahead_indent || last_line_num < 1
|
1668
|
628 if previous_line =~# '\%(;\|^\s*}\)'.endline || last_line_num < 1
|
532
|
629 break
|
|
630 endif
|
|
631 endif
|
|
632 endif
|
|
633 endwhile
|
|
634
|
|
635 if indent(last_match) != ind
|
|
636 let ind = indent(last_match)
|
|
637 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting
|
|
638
|
|
639 if cline =~# defaultORcase
|
|
640 let ind = ind - &sw
|
|
641 endif
|
|
642 return ind
|
|
643 endif
|
|
644 endif
|
|
645
|
|
646 let plinnum = GetLastRealCodeLNum(lnum - 1)
|
|
647 let pline = getline(plinnum)
|
|
648
|
|
649 let last_line = substitute(last_line,"\\(//\\|#\\)\\(\\(\\([^\"']*\\([\"']\\)[^\"']*\\5\\)\\+[^\"']*$\\)\\|\\([^\"']*$\\)\\)",'','')
|
|
650
|
|
651
|
|
652 if ind == b:PHP_default_indenting
|
|
653 if last_line =~ terminated
|
|
654 let LastLineClosed = 1
|
|
655 endif
|
|
656 endif
|
|
657
|
|
658 if !LastLineClosed
|
|
659
|
2034
|
660
|
532
|
661 if last_line =~# '[{(]'.endline || last_line =~? '\h\w*\s*(.*,$' && pline !~ '[,(]'.endline
|
|
662
|
|
663 if !b:PHP_BracesAtCodeLevel || last_line !~# '^\s*{'
|
409
|
664 let ind = ind + &sw
|
532
|
665 endif
|
|
666
|
1668
|
667 if b:PHP_BracesAtCodeLevel || b:PHP_vintage_case_default_indent == 1 || cline !~# defaultORcase
|
532
|
668 let b:PHP_CurrentIndentLevel = ind
|
1668
|
669
|
532
|
670 return ind
|
|
671 endif
|
|
672
|
|
673 elseif last_line =~ '\S\+\s*),'.endline
|
|
674 call cursor(lnum, 1)
|
|
675 call search('),'.endline, 'W')
|
|
676 let openedparent = searchpair('(', '', ')', 'bW', 'Skippmatch()')
|
|
677 if openedparent != lnum
|
|
678 let ind = indent(openedparent)
|
|
679 endif
|
1668
|
680 elseif last_line =~ '^\s*'.s:blockstart
|
|
681 let ind = ind + &sw
|
|
682
|
2034
|
683 elseif last_line =~# defaultORcase && cline !~# defaultORcase
|
1668
|
684 let ind = ind + &sw
|
532
|
685
|
|
686
|
2034
|
687 elseif pline =~ '\%(;\%(\s*?>\)\=\|<<<''\=\a\w*''\=$\|^\s*}\|{\)'.endline . '\|' . defaultORcase && cline !~# defaultORcase
|
532
|
688
|
|
689 let ind = ind + &sw
|
409
|
690 endif
|
7
|
691
|
532
|
692 endif
|
409
|
693
|
532
|
694 if cline =~ '^\s*);\='
|
|
695 let ind = ind - &sw
|
1668
|
696 elseif cline =~# defaultORcase && last_line !~# defaultORcase
|
532
|
697 let ind = ind - &sw
|
|
698
|
|
699 endif
|
|
700
|
|
701 let b:PHP_CurrentIndentLevel = ind
|
|
702 return ind
|
7
|
703 endfunction
|