3 " Author: John Wellesz <John.wellesz (AT) teaser (DOT) fr>
4 " URL: http://www.2072productions.com/vim/indent/php.vim
5 " Last Change: 2008 November 22nd
6 " Newsletter: http://www.2072productions.com/?to=php-indent-for-vim-newsletter.php
10 " If you find a bug, please e-mail me at John.wellesz (AT) teaser (DOT) fr
11 " with an example of code that breaks the algorithm.
14 " Thanks a lot for using this script.
17 " NOTE: This script must be used with PHP syntax ON and with the php syntax
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 )
20 " the later is bunbdled by default with Vim 7.
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 ';').
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
33 " So you have to remove those useless characters first with a command like:
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).
40 " Options: See :help php-indent for available options.
43 if exists("b:did_indent")
49 let php_sync_method = 0
52 if exists("PHP_default_indenting")
53 let b:PHP_default_indenting = PHP_default_indenting * &sw
55 let b:PHP_default_indenting = 0
58 if exists("PHP_BracesAtCodeLevel")
59 let b:PHP_BracesAtCodeLevel = PHP_BracesAtCodeLevel
61 let b:PHP_BracesAtCodeLevel = 0
65 if exists("PHP_autoformatcomment")
66 let b:PHP_autoformatcomment = PHP_autoformatcomment
68 let b:PHP_autoformatcomment = 1
71 if exists("PHP_vintage_case_default_indent")
72 let b:PHP_vintage_case_default_indent = PHP_vintage_case_default_indent
74 let b:PHP_vintage_case_default_indent = 0
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
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
93 setlocal nosmartindent
98 setlocal indentexpr=GetPhpIndent()
99 setlocal indentkeys=0{,0},0),:,!^F,o,O,e,*<Return>,=?>,=<?,=*/
103 let s:searchpairflags = 'bWr'
105 if &fileformat == "unix" && exists("PHP_removeCRwhenUnix") && PHP_removeCRwhenUnix
109 if exists("*GetPhpIndent")
110 call ResetPhpOptions()
114 let s:endline= '\s*\%(//.*\|#.*\|/\*.*\*/\s*\)\=$'
115 let s:PHP_startindenttag = '<?\%(.*?>\)\@!\|<script[^>]*>\%(.*<\/script>\)\@!'
116 "setlocal debug=msg " XXX
119 function! GetLastRealCodeLNum(startline) " {{{
121 let lnum = a:startline
123 if b:GetLastRealCodeLNum_ADD && b:GetLastRealCodeLNum_ADD == lnum + 1
124 let lnum = b:GetLastRealCodeLNum_ADD
130 let lnum = prevnonblank(lnum)
131 let lastline = getline(lnum)
133 if b:InPHPcode_and_script && lastline =~ '?>\s*$'
135 elseif lastline =~ '^\s*?>.*<?\%(php\)\=\s*$'
137 elseif lastline =~ '^\s*\%(//\|#\|/\*.*\*/\s*$\)'
139 elseif lastline =~ '\*/\s*$'
141 if lastline !~ '^\*/'
142 call search('\*/', 'W')
144 let lnum = searchpair('/\*', '', '\*/', s:searchpairflags, 'Skippmatch2()')
146 let lastline = getline(lnum)
147 if lastline =~ '^\s*/\*'
154 elseif lastline =~? '\%(//\s*\|?>.*\)\@<!<?\%(php\)\=\s*$\|^\s*<script\>'
156 while lastline !~ '\(<?.*\)\@<!?>' && lnum > 1
158 let lastline = getline(lnum)
160 if lastline =~ '^\s*?>'
167 elseif lastline =~? '^\a\w*;\=$' && lastline !~? s:notPhpHereDoc " XXX 0607
168 let tofind=substitute( lastline, '\(\a\w*\);\=', '<<<''\\=\1''\\=$', '') " XXX 0607
169 while getline(lnum) !~? tofind && lnum > 1
177 if lnum==1 && getline(lnum)!~ '<?'
181 if b:InPHPcode_and_script && !b:InPHPcode
182 let b:InPHPcode_and_script = 0
190 function! Skippmatch2()
192 let line = getline(".")
194 if line =~ '\%(".*\)\@<=/\*\%(.*"\)\@=' || line =~ '\%(\%(//\|#\).*\)\@<=/\*'
201 function! Skippmatch() " {{{
202 let synname = synIDattr(synID(line("."), col("."), 0), "name")
203 if synname == "Delimiter" || synname == "phpRegionDelimiter" || synname =~# "^phpParent" || synname == "phpArrayParens" || synname =~# '^php\%(Block\|Brace\)' || synname == "javaScriptBraces" || synname =~# "^phpComment" && b:UserIsTypingComment
210 function! FindOpenBracket(lnum) " {{{
211 call cursor(a:lnum, 1)
212 return searchpair('{', '', '}', 'bW', 'Skippmatch()')
215 function! FindTheIfOfAnElse (lnum, StopAfterFirstPrevElse) " {{{
217 if getline(a:lnum) =~# '^\s*}\s*else\%(if\)\=\>'
218 let beforeelse = a:lnum
220 let beforeelse = GetLastRealCodeLNum(a:lnum - 1)
227 if getline(beforeelse) =~# '^\s*\%(}\s*\)\=else\%(\s*if\)\@!\>'
228 let s:iftoskip = s:iftoskip + 1
231 if getline(beforeelse) =~ '^\s*}'
232 let beforeelse = FindOpenBracket(beforeelse)
234 if getline(beforeelse) =~ '^\s*{'
235 let beforeelse = GetLastRealCodeLNum(beforeelse - 1)
240 if !s:iftoskip && a:StopAfterFirstPrevElse && getline(beforeelse) =~# '^\s*\%([}]\s*\)\=else\%(if\)\=\>'
244 if getline(beforeelse) !~# '^\s*if\>' && beforeelse>1 || s:iftoskip && beforeelse>1
246 if s:iftoskip && getline(beforeelse) =~# '^\s*if\>'
247 let s:iftoskip = s:iftoskip - 1
250 let s:level = s:level + 1
251 let beforeelse = FindTheIfOfAnElse(beforeelse, a:StopAfterFirstPrevElse)
258 function! IslinePHP (lnum, tofind) " {{{
259 let cline = getline(a:lnum)
262 let tofind = "^\\s*[\"']*\\s*\\zs\\S"
264 let tofind = a:tofind
267 let tofind = tofind . '\c'
269 let coltotest = match (cline, tofind) + 1
271 let synname = synIDattr(synID(a:lnum, coltotest, 0), "name")
273 if synname =~ '^php' || synname=="Delimiter" || synname =~? '^javaScript'
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\>\)'
283 let s:autoresetoptions = 0
284 if ! s:autoresetoptions
285 let s:autoresetoptions = 1
288 function! ResetPhpOptions()
289 if ! b:optionsset && &filetype == "php"
290 if b:PHP_autoformatcomment
292 setlocal comments=s1:/*,mb:*,ex:*/,://,:#
294 setlocal formatoptions-=t
295 setlocal formatoptions+=q
296 setlocal formatoptions+=r
297 setlocal formatoptions+=o
298 setlocal formatoptions+=w
299 setlocal formatoptions+=c
300 setlocal formatoptions+=b
306 call ResetPhpOptions()
308 function! GetPhpIndent()
310 let b:GetLastRealCodeLNum_ADD = 0
313 if b:PHP_oldchangetick != b:changedtick
314 let b:PHP_oldchangetick = b:changedtick
318 if b:PHP_default_indenting
319 let b:PHP_default_indenting = g:PHP_default_indenting * &sw
322 let cline = getline(v:lnum)
324 if !b:PHP_indentinghuge && b:PHP_lastindented > b:PHP_indentbeforelast
325 if b:PHP_indentbeforelast
326 let b:PHP_indentinghuge = 1
327 echom 'Large indenting detected, speed optimizations engaged (v1.30)'
329 let b:PHP_indentbeforelast = b:PHP_lastindented
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
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
344 let b:InPHPcode_checked = 0
345 let b:InPHPcode_and_script = 0
346 let b:InPHPcode_tofind = ""
348 elseif v:lnum > b:PHP_lastindented
349 let real_PHP_lastindented = b:PHP_lastindented
350 let b:PHP_lastindented = v:lnum
354 if !b:InPHPcode_checked " {{{ One time check
355 let b:InPHPcode_checked = 1
359 let synname = IslinePHP (prevnonblank(v:lnum), "")
363 if synname != "phpHereDoc" && synname != "phpHereDocDelimiter"
365 let b:InPHPcode_tofind = ""
367 if synname =~# "^phpComment"
368 let b:UserIsTypingComment = 1
370 let b:UserIsTypingComment = 0
373 if synname =~? '^javaScript'
374 let b:InPHPcode_and_script = 1
379 let b:UserIsTypingComment = 0
381 let lnum = v:lnum - 1
382 while getline(lnum) !~? '<<<''\=\a\w*''\=$' && lnum > 1
386 let b:InPHPcode_tofind = substitute( getline(lnum), '^.*<<<''\=\(\a\w*\)''\=$', '^\\s*\1;\\=$', '') " XXX 0607
390 let b:UserIsTypingComment = 0
391 let b:InPHPcode_tofind = '<?\%(.*?>\)\@!\|<script.*>'
393 endif "!b:InPHPcode_checked }}}
396 " Test if we are indenting PHP code {{{
397 let lnum = prevnonblank(v:lnum - 1)
398 let last_line = getline(lnum)
400 if b:InPHPcode_tofind!=""
401 if cline =~? b:InPHPcode_tofind
403 let b:InPHPcode_tofind = ""
404 let b:UserIsTypingComment = 0
406 call cursor(v:lnum, 1)
408 call search('\*/', 'W')
410 let lnum = searchpair('/\*', '', '\*/', s:searchpairflags, 'Skippmatch2()')
412 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting
414 let b:PHP_LastIndentedWasComment = 0
416 if cline =~ '^\s*\*/'
417 return indent(lnum) + 1
422 elseif cline =~? '<script\>'
423 let b:InPHPcode_and_script = 1
424 let b:GetLastRealCodeLNum_ADD = v:lnum
431 if !b:InPHPcode_and_script && last_line =~ '\%(<?.*\)\@<!?>\%(.*<?\)\@!' && IslinePHP(lnum, '?>')=~"Delimiter"
432 if cline !~? s:PHP_startindenttag
434 let b:InPHPcode_tofind = s:PHP_startindenttag
435 elseif cline =~? '<script\>'
436 let b:InPHPcode_and_script = 1
439 elseif last_line =~? '<<<''\=\a\w*''\=$' " XXX 0607
441 let b:InPHPcode_tofind = substitute( last_line, '^.*<<<''\=\(\a\w*\)''\=$', '^\\s*\1;\\=$', '') " XXX 0607
443 elseif !UserIsEditing && cline =~ '^\s*/\*\%(.*\*/\)\@!' && getline(v:lnum + 1) !~ '^\s*\*'
445 let b:InPHPcode_tofind = '\*/'
447 elseif cline =~? '^\s*</script>'
449 let b:InPHPcode_tofind = s:PHP_startindenttag
454 if !b:InPHPcode && !b:InPHPcode_and_script
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)
463 let b:PHP_LastIndentedWasComment = 1
465 let b:PHP_LastIndentedWasComment = 0
468 " Indent multiline /* comments correctly {{{
470 if b:PHP_InsideMultilineComment || b:UserIsTypingComment
471 if cline =~ '^\s*\*\%(\/\)\@!'
472 if last_line =~ '^\s*/\*'
473 return indent(lnum) + 1
478 let b:PHP_InsideMultilineComment = 0
482 if !b:PHP_InsideMultilineComment && cline =~ '^\s*/\*' && cline !~ '\*/\s*$'
483 if getline(v:lnum + 1) !~ '^\s*\*'
486 let b:PHP_InsideMultilineComment = 1
490 " Things always indented at col 1 (PHP delimiter: <?, ?>, Heredoc end) {{{
491 if cline =~# '^\s*<?' && cline !~ '?>'
495 if cline =~ '^\s*?>' && cline !~# '<?'
499 if cline =~? '^\s*\a\w*;$\|^\a\w*$' && cline !~? s:notPhpHereDoc " XXX 0607
505 let lnum = GetLastRealCodeLNum(v:lnum - 1)
507 let last_line = getline(lnum)
508 let ind = indent(lnum)
509 let endline= s:endline
511 if ind==0 && b:PHP_default_indenting
512 let ind = b:PHP_default_indenting
516 return b:PHP_default_indenting
520 if cline =~ '^\s*}\%(}}\)\@!'
521 let ind = indent(FindOpenBracket(v:lnum))
522 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting
526 if cline =~ '^\s*\*/'
527 call cursor(v:lnum, 1)
529 call search('\*/', 'W')
531 let lnum = searchpair('/\*', '', '\*/', s:searchpairflags, 'Skippmatch2()')
533 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting
535 if cline =~ '^\s*\*/'
536 return indent(lnum) + 1
542 let defaultORcase = '^\s*\%(default\|case\).*:'
544 if last_line =~ '[;}]'.endline && last_line !~ '^)' && last_line !~# defaultORcase " Added && last_line !~ '^)' on 2007-12-30
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
552 let LastLineClosed = 0
554 let terminated = '\%(;\%(\s*?>\)\=\|<<<''\=\a\w*''\=$\|^\s*}\)'.endline " XXX 0607
556 let unstated = '\%(^\s*'.s:blockstart.'.*)\|\%(//.*\)\@<!\<e'.'lse\>\)'.endline
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))
561 elseif cline =~ '^\s*)\=\s*{'
562 let previous_line = last_line
563 let last_line_num = lnum
565 while last_line_num > 1
567 if previous_line =~ '^\s*\%(' . s:blockstart . '\|\%([a-zA-Z]\s*\)*function\)'
569 let ind = indent(last_line_num)
571 if b:PHP_BracesAtCodeLevel
578 let last_line_num = last_line_num - 1
579 let previous_line = getline(last_line_num)
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
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)
587 let previous_line = last_line
588 let last_line_num = lnum
589 let LastLineClosed = 1
592 if previous_line =~ '^\s*}'
593 let last_line_num = FindOpenBracket(last_line_num)
595 if getline(last_line_num) =~ '^\s*{'
596 let last_line_num = GetLastRealCodeLNum(last_line_num - 1)
599 let previous_line = getline(last_line_num)
604 if getline(last_line_num) =~# '^\s*else\%(if\)\=\>'
605 let last_line_num = FindTheIfOfAnElse(last_line_num, 0)
610 let last_match = last_line_num
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)
619 if previous_line =~# defaultORcase.'\|{'.endline
623 if after_previous_line=~# '^\s*'.s:blockstart.'.*)'.endline && previous_line =~# '[;}]'.endline
627 if one_ahead_indent == two_ahead_indent || last_line_num < 1
628 if previous_line =~# '\%(;\|^\s*}\)'.endline || last_line_num < 1
635 if indent(last_match) != ind
636 let ind = indent(last_match)
637 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting
639 if cline =~# defaultORcase
646 let plinnum = GetLastRealCodeLNum(lnum - 1)
647 let pline = getline(plinnum)
649 let last_line = substitute(last_line,"\\(//\\|#\\)\\(\\(\\([^\"']*\\([\"']\\)[^\"']*\\5\\)\\+[^\"']*$\\)\\|\\([^\"']*$\\)\\)",'','')
652 if ind == b:PHP_default_indenting
653 if last_line =~ terminated
654 let LastLineClosed = 1
661 if last_line =~# '[{(]'.endline || last_line =~? '\h\w*\s*(.*,$' && pline !~ '[,(]'.endline
663 if !b:PHP_BracesAtCodeLevel || last_line !~# '^\s*{'
667 if b:PHP_BracesAtCodeLevel || b:PHP_vintage_case_default_indent == 1 || cline !~# defaultORcase
668 let b:PHP_CurrentIndentLevel = ind
673 elseif last_line =~ '\S\+\s*),'.endline
675 call search('),'.endline, 'W')
676 let openedparent = searchpair('(', '', ')', 'bW', 'Skippmatch()')
677 if openedparent != lnum
678 let ind = indent(openedparent)
680 elseif last_line =~ '^\s*'.s:blockstart
683 elseif last_line =~# defaultORcase && cline !~# defaultORcase
687 elseif pline =~ '\%(;\%(\s*?>\)\=\|<<<''\=\a\w*''\=$\|^\s*}\|{\)'.endline . '\|' . defaultORcase && cline !~# defaultORcase
694 if cline =~ '^\s*);\='
696 elseif cline =~# defaultORcase && last_line !~# defaultORcase
701 let b:PHP_CurrentIndentLevel = ind