" URL: http://www.2072productions.com/vim/indent/php.vim
" Home: https://github.com/2072/PHP-Indenting-for-VIm
" Last Change: 2013 August 7th
" Version: 1.39
"
"
" Type :help php-indent for available options
"
" A fully commented version of this file is available on github
"
"
" If you find a bug, please open a ticket on github.org
" ( https://github.com/2072/PHP-Indenting-for-VIm/issues ) with an example of
" code that breaks the algorithm.
"
" NOTE: This script must be used with PHP syntax ON and with the php syntax
" script by Lutz Eymers (http://www.isp.de/data/php.vim ) or with the
" script by Peter Hodge (http://www.vim.org/scripts/script.php?script_id=1571 )
" the later is bunbdled by default with Vim 7.
"
"
" In the case you have syntax errors in your script such as HereDoc end
" identifiers not at col 1 you'll have to indent your file 2 times (This
" script will automatically put HereDoc end identifiers at col 1 if
" they are followed by a ';').
"
" NOTE: If you are editing files in Unix file format and that (by accident)
" there are '\r' before new lines, this script won't be able to proceed
" correctly and will make many mistakes because it won't be able to match
" '\s*$' correctly.
" So you have to remove those useless characters first with a command like:
"
" :%s /\r$//g
"
" or simply 'let' the option PHP_removeCRwhenUnix to 1 and the script will
" silently remove them when VIM load this script (at each bufread).
"
if exists("b:did_indent")
finish
endif
let b:did_indent = 1
let php_sync_method = 0
if exists("PHP_default_indenting")
let b:PHP_default_indenting = PHP_default_indenting * &sw
else
let b:PHP_default_indenting = 0
endif
if exists("PHP_outdentSLComments")
let b:PHP_outdentSLComments = PHP_outdentSLComments * &sw
else
let b:PHP_outdentSLComments = 0
endif
if exists("PHP_BracesAtCodeLevel")
let b:PHP_BracesAtCodeLevel = PHP_BracesAtCodeLevel
else
let b:PHP_BracesAtCodeLevel = 0
endif
if exists("PHP_autoformatcomment")
let b:PHP_autoformatcomment = PHP_autoformatcomment
else
let b:PHP_autoformatcomment = 1
endif
if exists("PHP_outdentphpescape")
let b:PHP_outdentphpescape = PHP_outdentphpescape
else
let b:PHP_outdentphpescape = 1
endif
if exists("PHP_vintage_case_default_indent") && PHP_vintage_case_default_indent
let b:PHP_vintage_case_default_indent = 1
else
let b:PHP_vintage_case_default_indent = 0
endif
let b:PHP_lastindented = 0
let b:PHP_indentbeforelast = 0
let b:PHP_indentinghuge = 0
let b:PHP_CurrentIndentLevel = b:PHP_default_indenting
let b:PHP_LastIndentedWasComment = 0
let b:PHP_InsideMultilineComment = 0
let b:InPHPcode = 0
let b:InPHPcode_checked = 0
let b:InPHPcode_and_script = 0
let b:InPHPcode_tofind = ""
let b:PHP_oldchangetick = b:changedtick
let b:UserIsTypingComment = 0
let b:optionsset = 0
setlocal nosmartindent
setlocal noautoindent
setlocal nocindent
setlocal nolisp
setlocal indentexpr=GetPhpIndent()
setlocal indentkeys=0{,0},0),:,!^F,o,O,e,*,=?>,=,=*/
let s:searchpairflags = 'bWr'
if &fileformat == "unix" && exists("PHP_removeCRwhenUnix") && PHP_removeCRwhenUnix
silent! %s/\r$//g
endif
if exists("*GetPhpIndent")
call ResetPhpOptions()
finish " XXX -- comment this line for easy dev
endif
let s:endline= '\s*\%(//.*\|#.*\|/\*.*\*/\s*\)\=$'
let s:PHP_startindenttag = '\%(.*?>\)\@!\|'
let b:InPHPcode = 0
let b:InPHPcode_tofind = s:PHP_startindenttag
endif
endif " }}}
if !b:InPHPcode && !b:InPHPcode_and_script
return -1
endif
" Indent successive // or # comment the same way the first is {{{
let addSpecial = 0
if cline =~ '^\s*\%(//\|#\|/\*.*\*/\s*$\)'
let addSpecial = b:PHP_outdentSLComments
if b:PHP_LastIndentedWasComment == 1
return indent(real_PHP_lastindented)
endif
let b:PHP_LastIndentedWasComment = 1
else
let b:PHP_LastIndentedWasComment = 0
endif " }}}
" Indent multiline /* comments correctly {{{
if b:PHP_InsideMultilineComment || b:UserIsTypingComment
if cline =~ '^\s*\*\%(\/\)\@!'
if last_line =~ '^\s*/\*'
return indent(lnum) + 1
else
return indent(lnum)
endif
else
let b:PHP_InsideMultilineComment = 0
endif
endif
if !b:PHP_InsideMultilineComment && cline =~ '^\s*/\*' && cline !~ '\*/\s*$'
if getline(v:lnum + 1) !~ '^\s*\*'
return -1
endif
let b:PHP_InsideMultilineComment = 1
endif " }}}
" Things always indented at col 1 (PHP delimiter: , ?>, Heredoc end) {{{
if cline =~# '^\s*' && cline !~ '?>' && b:PHP_outdentphpescape
return 0
endif
if cline =~ '^\s*?>' && cline !~# '' && b:PHP_outdentphpescape
return 0
endif
if cline =~? '^\s*\a\w*;$\|^\a\w*$' && cline !~? s:notPhpHereDoc
return 0
endif " }}}
let s:level = 0
let lnum = GetLastRealCodeLNum(v:lnum - 1)
let last_line = getline(lnum)
let ind = indent(lnum)
let endline= s:endline
if ind==0 && b:PHP_default_indenting
let ind = b:PHP_default_indenting
endif
if lnum == 0
return b:PHP_default_indenting + addSpecial
endif
if cline =~ '^\s*}\%(}}\)\@!'
let ind = indent(FindOpenBracket(v:lnum))
let b:PHP_CurrentIndentLevel = b:PHP_default_indenting
return ind
endif
if cline =~ '^\s*\*/'
call cursor(v:lnum, 1)
if cline !~ '^\*/'
call search('\*/', 'W')
endif
let lnum = searchpair('/\*', '', '\*/', s:searchpairflags, 'Skippmatch2()')
let b:PHP_CurrentIndentLevel = b:PHP_default_indenting
if cline =~ '^\s*\*/'
return indent(lnum) + 1
else
return indent(lnum)
endif
endif
if last_line =~ '[;}]'.endline && last_line !~ '^[)\]]' && last_line !~# s:defaultORcase
if ind==b:PHP_default_indenting
return b:PHP_default_indenting + addSpecial
elseif b:PHP_indentinghuge && ind==b:PHP_CurrentIndentLevel && cline !~# '^\s*\%(else\|\%(case\|default\).*:\|[})];\=\)' && last_line !~# '^\s*\%(\%(}\s*\)\=else\)' && getline(GetLastRealCodeLNum(lnum - 1))=~';'.endline
return b:PHP_CurrentIndentLevel + addSpecial
endif
endif
let LastLineClosed = 0
let terminated = '\%(;\%(\s*\%(?>\|}\)\)\=\|<<<''\=\a\w*''\=$\|^\s*}\)'.endline
let unstated = '\%(^\s*'.s:blockstart.'.*)\|\%(//.*\)\@\)'.endline
if ind != b:PHP_default_indenting && cline =~# '^\s*else\%(if\)\=\>'
let b:PHP_CurrentIndentLevel = b:PHP_default_indenting
return indent(FindTheIfOfAnElse(v:lnum, 1))
elseif cline =~# s:defaultORcase
return FindTheSwitchIndent(v:lnum) + &sw * b:PHP_vintage_case_default_indent
elseif cline =~ '^\s*)\=\s*{'
let previous_line = last_line
let last_line_num = lnum
while last_line_num > 1
if previous_line =~ '^\s*\%(' . s:blockstart . '\|\%([a-zA-Z]\s*\)*function\)'
let ind = indent(last_line_num)
if b:PHP_BracesAtCodeLevel
let ind = ind + &sw
endif
return ind
endif
let last_line_num = last_line_num - 1
let previous_line = getline(last_line_num)
endwhile
elseif last_line =~# unstated && cline !~ '^\s*);\='.endline
let ind = ind + &sw " we indent one level further when the preceding line is not stated
return ind + addSpecial
elseif (ind != b:PHP_default_indenting || last_line =~ '^[)\]]' ) && last_line =~ terminated " Added || last_line =~ '^)' on 2007-12-30 (array indenting problem broke other things)
let previous_line = last_line
let last_line_num = lnum
let LastLineClosed = 1
while 1
if previous_line =~ '^\s*}\|;\s*}'.endline " XXX
call cursor(last_line_num, 1)
call search('}\|;\s*}'.endline, 'W')
let oldLastLine = last_line_num
let last_line_num = searchpair('{', '', '}', 'bW', 'Skippmatch()')
if oldLastLine == last_line_num || getline(last_line_num) =~ '^\s*{'
let last_line_num = GetLastRealCodeLNum(last_line_num - 1)
endif
let previous_line = getline(last_line_num)
continue
else
if getline(last_line_num) =~# '^\s*else\%(if\)\=\>'
let last_line_num = FindTheIfOfAnElse(last_line_num, 0)
continue
endif
let last_match = last_line_num
let one_ahead_indent = indent(last_line_num)
let last_line_num = GetLastRealCodeLNum(last_line_num - 1)
let two_ahead_indent = indent(last_line_num)
let after_previous_line = previous_line
let previous_line = getline(last_line_num)
if previous_line =~# s:defaultORcase.'\|{'.endline
break
endif
if after_previous_line=~# '^\s*'.s:blockstart.'.*)'.endline && previous_line =~# '[;}]'.endline
break
endif
if one_ahead_indent == two_ahead_indent || last_line_num < 1
if previous_line =~# '\%(;\|^\s*}\)'.endline || last_line_num < 1
break
endif
endif
endif
endwhile
if indent(last_match) != ind
let ind = indent(last_match)
let b:PHP_CurrentIndentLevel = b:PHP_default_indenting
return ind + addSpecial
endif
endif
let plinnum = GetLastRealCodeLNum(lnum - 1)
let AntepenultimateLine = getline(plinnum)
let last_line = substitute(last_line,"\\(//\\|#\\)\\(\\(\\([^\"']*\\([\"']\\)[^\"']*\\5\\)\\+[^\"']*$\\)\\|\\([^\"']*$\\)\\)",'','')
if ind == b:PHP_default_indenting
if last_line =~ terminated
let LastLineClosed = 1
endif
endif
if !LastLineClosed
if last_line =~# '[{(\[]'.endline || last_line =~? '\h\w*\s*(.*,$' && AntepenultimateLine !~ '[,(]'.endline
if !b:PHP_BracesAtCodeLevel || last_line !~# '^\s*{'
let ind = ind + &sw
endif
if b:PHP_BracesAtCodeLevel || b:PHP_vintage_case_default_indent == 1
let b:PHP_CurrentIndentLevel = ind
return ind + addSpecial
endif
elseif last_line =~ '\S\+\s*),'.endline
call cursor(lnum, 1)
call search('),'.endline, 'W')
let openedparent = searchpair('(', '', ')', 'bW', 'Skippmatch()')
if openedparent != lnum
let ind = indent(openedparent)
endif
elseif last_line =~ '^\s*'.s:blockstart
let ind = ind + &sw
elseif AntepenultimateLine =~ '\%(;\%(\s*\%(?>\|}\)\)\=\|<<<''\=\a\w*''\=$\|^\s*}\|{\)'.endline . '\|' . s:defaultORcase
let ind = ind + &sw
endif
endif
if cline =~ '^\s*[)\]];\='
let ind = ind - &sw
endif
let b:PHP_CurrentIndentLevel = ind
return ind + addSpecial
endfunction