robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
7be031f397
master
1" Language: PHP 2" Author: John Wellesz <John.wellesz (AT) gmail (DOT) com> 3" URL: https://www.2072productions.com/vim/indent/php.vim 4" Home: https://github.com/2072/PHP-Indenting-for-VIm 5" Last Change: 2025 November 16th 6" Version: 1.76 7" 8" 9" Type :help php-indent for available options 10" 11" A fully commented version of this file is available on github 12" 13" 14" If you find a bug, please open a ticket on github.com 15" ( https://github.com/2072/PHP-Indenting-for-VIm/issues ) with an example of 16" code that breaks the algorithm. 17" 18 19" NOTE: This script must be used with PHP syntax ON and with the php syntax 20" script by Lutz Eymers (http://www.isp.de/data/php.vim ) or with the 21" script by Peter Hodge (https://www.vim.org/scripts/script.php?script_id=1571 ) 22" the later is bunbdled by default with Vim 7. 23" 24" 25" In the case you have syntax errors in your script such as HereDoc end 26" identifiers not at col 1 you'll have to indent your file 2 times (This 27" script will automatically put HereDoc end identifiers at col 1 if 28" they are followed by a ';'). 29" 30 31" NOTE: If you are editing files in Unix file format and that (by accident) 32" there are '\r' before new lines, this script won't be able to proceed 33" correctly and will make many mistakes because it won't be able to match 34" '\s*$' correctly. 35" So you have to remove those useless characters first with a command like: 36" 37" :%s /\r$//g 38" 39" or simply 'let' the option PHP_removeCRwhenUnix to 1 and the script will 40" silently remove them when VIM load this script (at each bufread). 41 42 43if exists ( "b:did_indent" ) 44finish 45endif 46let b: did_indent = 1 47 48 49let g: php_sync_method = 0 50 51 52if exists ( "PHP_default_indenting" ) 53let b: PHP_default_indenting = PHP_default_indenting * shiftwidth () 54else 55let b: PHP_default_indenting = 0 56endif 57 58if exists ( "PHP_outdentSLComments" ) 59let b: PHP_outdentSLComments = PHP_outdentSLComments * shiftwidth () 60else 61let b: PHP_outdentSLComments = 0 62endif 63 64if exists ( "PHP_BracesAtCodeLevel" ) 65let b: PHP_BracesAtCodeLevel = PHP_BracesAtCodeLevel 66else 67let b: PHP_BracesAtCodeLevel = 0 68endif 69 70 71if exists ( "PHP_autoformatcomment" ) 72let b: PHP_autoformatcomment = PHP_autoformatcomment 73else 74let b: PHP_autoformatcomment = 1 75endif 76 77if exists ( "PHP_outdentphpescape" ) 78let b: PHP_outdentphpescape = PHP_outdentphpescape 79else 80let b: PHP_outdentphpescape = 1 81endif 82 83if exists ( "PHP_noArrowMatching" ) 84let b: PHP_noArrowMatching = PHP_noArrowMatching 85else 86let b: PHP_noArrowMatching = 0 87endif 88 89 90if exists ( "PHP_vintage_case_default_indent" ) && PHP_vintage_case_default_indent 91let b: PHP_vintage_case_default_indent = 1 92else 93let b: PHP_vintage_case_default_indent = 0 94endif 95 96if exists ( "PHP_IndentFunctionCallParameters" ) 97let b: PHP_IndentFunctionCallParameters = PHP_IndentFunctionCallParameters 98else 99let b: PHP_IndentFunctionCallParameters = 0 100endif 101 102if exists ( "PHP_IndentFunctionDeclarationParameters" ) 103let b: PHP_IndentFunctionDeclarationParameters = PHP_IndentFunctionDeclarationParameters 104else 105let b: PHP_IndentFunctionDeclarationParameters = 0 106endif 107 108let b: PHP_lastindented = 0 109let b: PHP_indentbeforelast = 0 110let b: PHP_indentinghuge = 0 111let b: PHP_CurrentIndentLevel = b: PHP_default_indenting 112let b: PHP_LastIndentedWasComment = 0 113let b: PHP_InsideMultilineComment = 0 114let b: InPHPcode = 0 115let b: InPHPcode_checked = 0 116let b: InPHPcode_and_script = 0 117let b: InPHPcode_tofind = "" 118let b: PHP_oldchangetick = b: changedtick 119let b: UserIsTypingComment = 0 120let b: optionsset = 0 121 122setlocal nosmartindent 123setlocal noautoindent 124setlocal nocindent 125setlocal nolisp 126 127setlocal indentexpr = GetPhpIndent () 128setlocal indentkeys = 0{,0},0),0],:,!^F,o,O,e,*<Return>,=?>,=<?,=*/ 129 130let b: undo_indent = "setl ai< cin< inde< indk< lisp< si<" 131 132let s: searchpairflags = 'bWr' 133 134if & fileformat == "unix" && exists ( "PHP_removeCRwhenUnix" ) && PHP_removeCRwhenUnix 135silent ! %s /\r$//g 136endif 137 138if exists ( "*GetPhpIndent" ) 139call ResetPhpOptions () 140finish " XXX -- comment this line for easy dev 141endif 142 143 144let s: endline = '\s*\%(//.*\|#\[\@!.*\|/\*.*\*/\s*\)\=$' 145let s: PHP_validVariable = '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*' 146let s: notPhpHereDoc = '\<\%(break\|return\|continue\|exit\|die\|true\|false\|elseif\|else\|end\%(if\|while\|for\|foreach\|match\|switch\)\)\>' 147let s: blockstart = '\%(\%(\%(}\s*\)\=else\%(\s\+\)\=\)\=if\>\|\%(}\s*\)\?else\>\|do\>\|while\>\|match\>\|switch\>\|case\>\|default\>\|for\%(each\)\=\>\|declare\>\|class\>\|trait\>\|\%()\s*\)\=use\>\|interface\>\|abstract\>\|final\>\|try\>\|\%(}\s*\)\=catch\>\|\%(}\s*\)\=finally\>\)' 148let s: functionDeclPrefix = '\<function\>\%(\s\+&\=' . s: PHP_validVariable . '\)\=\s*(' 149let s: functionDecl = s: functionDeclPrefix . '.*' 150let s: multilineFunctionDecl = s: functionDeclPrefix . s: endline 151let s: arrayDecl = '\<array\>\s*(.*' 152let s: multilineFunctionCall = s: PHP_validVariable . '\s*(' . s: endline 153let s: unstated = '\%(^\s*' . s: blockstart . '.*)\|\%(//.*\)\@<!\<e' . 'lse\>\)' . s: endline 154 155 156let s: terminated = '\%(\%(;\%(\s*\%(?>\|}\)\)\=\|<<<\s*[''"]\=\a\w*[''"]\=$\|^\s*}\|^\s*' . s: PHP_validVariable . ':\)' . s: endline . '\)' 157let s: PHP_startindenttag = '<?\%(.*?>\)\@!\|<script[^>]*>\%(.*<\/script>\)\@!' 158let s: matchStart = 'match\s*(\s*\$\?' . s: PHP_validVariable . '\s*)\s*{' . s: endline 159let s: structureHead = '^\s*\%(' . s: blockstart . '\)\|' . s: functionDecl . s: endline . '\|\<new\s\+class\>\|' . s: matchStart 160 161 162let s: escapeDebugStops = 0 163function ! DebugPrintReturn ( scriptLine ) 164 165if ! s: escapeDebugStops 166echo "debug:" . a: scriptLine 167let c = getchar () 168if c == "\<Del>" 169let s: escapeDebugStops = 1 170end 171endif 172 173endfunction 174 175function ! GetLastRealCodeLNum ( startline ) " {{{ 176 177let lnum = a: startline 178 179if b: GetLastRealCodeLNum_ADD && b: GetLastRealCodeLNum_ADD == lnum + 1 180let lnum = b: GetLastRealCodeLNum_ADD 181endif 182 183while lnum > 1 184let lnum = prevnonblank ( lnum ) 185let lastline = getline ( lnum ) 186 187if b: InPHPcode_and_script && lastline =~ '?>\s*$' 188let lnum = lnum - 1 189elseif lastline =~ '^\s*?>.*<?\%(php\)\=\s*$' 190let lnum = lnum - 1 191elseif lastline =~ '^\s*\%(//\|#\|/\*.*\*/\s*$\)' 192let lnum = lnum - 1 193elseif lastline =~ '\*/\s*$' 194call cursor ( lnum , 1 ) 195if lastline !~ '^\*/' 196call search ( '\*/' , 'W' ) 197endif 198let lnum = searchpair ( '/\*' , '' , '\*/' , s: searchpairflags , 'Skippmatch2()' ) 199 200let lastline = getline ( lnum ) 201if lastline =~ '^\s*/\*' 202let lnum = lnum - 1 203else 204break 205endif 206 207 208elseif lastline =~? '\%(//\s*\|?>.*\)\@<!<?\%(php\)\=\s*$\|^\s*<script\>' 209 210while lastline !~ '\(<?.*\)\@<!?>' && lnum > 1 211let lnum = lnum - 1 212let lastline = getline ( lnum ) 213endwhile 214if lastline =~ '^\s*?>' 215let lnum = lnum - 1 216else 217break 218endif 219 220 221elseif lastline =~? '^\a\w*;\=$' && lastline !~? s: notPhpHereDoc 222let tofind = substitute ( lastline , '\(\a\w*\);\=' , '<<<\\s*[''"]\\=\1[''"]\\=$' , '' ) 223while getline ( lnum ) !~? tofind && lnum > 1 224let lnum = lnum - 1 225endwhile 226elseif lastline =~ '^\s*[''"`][;,]' . s: endline || ( lastline =~ '^[^''"`]*[''"`][;,]' . s: endline && IslinePHP ( lnum , "" ) == "SpecStringEntrails" ) 227 228let tofind = substitute ( lastline , '^.*\([''"`]\)[;,].*$' , '^[^\1]\\+[\1]$\\|^[^\1]\\+[=([]\\s*[\1]' , '' ) 229let trylnum = lnum 230while getline ( trylnum ) !~? tofind && trylnum > 1 231let trylnum = trylnum - 1 232endwhile 233 234if trylnum == 1 235break 236else 237if lastline =~ ';' . s: endline 238while getline ( trylnum ) !~? s: terminated && getline ( trylnum ) !~? '{' . s: endline && trylnum > 1 239let trylnum = prevnonblank ( trylnum - 1 ) 240endwhile 241 242 243if trylnum == 1 244break 245end 246end 247let lnum = trylnum 248end 249else 250break 251endif 252endwhile 253 254if lnum == 1 && getline ( lnum ) !~ '<?' 255let lnum = 0 256endif 257 258if b: InPHPcode_and_script && 1 > b: InPHPcode 259let b: InPHPcode_and_script = 0 260endif 261 262return lnum 263endfunction " }}} 264 265function ! Skippmatch2 () 266 267let line = getline ( "." ) 268 269if line =~ "\\([\"']\\).*/\\*.*\\1" || line =~ '\%(//\|#\[\@!\).*/\*' 270return 1 271else 272return 0 273endif 274endfun 275 276function ! Skippmatch () " {{{ 277let synname = synIDattr ( synID ( line ( "." ), col ( "." ), 0 ), "name" ) 278if synname ==? "Delimiter" || synname ==? "phpRegionDelimiter" || synname =~? "^phpParent" || synname ==? "phpArrayParens" || synname =~? '^php\%(Block\|Brace\)' || synname ==? "javaScriptBraces" || synname =~? '^php\%(Doc\)\?Comment' && b: UserIsTypingComment 279return 0 280else 281return 1 282endif 283endfun " }}} 284 285function ! FindOpenBracket ( lnum , blockStarter ) " {{{ 286call cursor ( a: lnum , 1 ) 287let line = searchpair ( '{' , '' , '}' , 'bW' , 'Skippmatch()' ) 288 289if a: blockStarter == 1 290while line > 1 291let linec = getline ( line ) 292 293if linec =~ s: terminated || linec =~ s: structureHead 294break 295endif 296 297let line = GetLastRealCodeLNum ( line - 1 ) 298endwhile 299endif 300 301return line 302endfun " }}} 303 304let s: blockChars = { '{' : 1 , '[' : 1 , '(' : 1 , ')' : -1 , ']' : -1 , '}' : -1 } 305let s: blockCharsLUT = { '{' : '{' , '}' : '{' , '[' : '[' , ']' : '[' , '(' : '(' , ')' : '(' } 306function ! BalanceDirection ( str ) 307 308let balance = { '{' : 0 , '[' : 0 , '(' : 0 , 'none' : 0 } 309let director = 'none' 310 311for c in split ( a: str , '\zs' ) 312if has_key ( s: blockChars , c ) 313let balance [ s: blockCharsLUT [ c ]] += s: blockChars [ c ] 314 315if balance [ s: blockCharsLUT [ c ]] 316let director = s: blockCharsLUT [ c ] 317endif 318endif 319endfor 320 321return balance [ director ] 322endfun 323 324function ! StripEndlineComments ( line ) 325 326let cleaned = substitute ( a: line , '\v(//|#\[\@!)((([^"'']*(["''])[^"'']*\5)+[^"'']*$)|([^"'']*$))' , '' , '' ) 327if cleaned != a: line 328endif 329return cleaned 330endfun 331 332function ! FindArrowIndent ( lnum ) " {{{ 333 334let parentArrowPos = -1 335let cursorPos = -1 336let lnum = a: lnum 337while lnum > 1 338let last_line = getline ( lnum ) 339if last_line =~ '^\s*->' 340let parentArrowPos = indent ( a: lnum ) 341break 342else 343 344if b: PHP_noArrowMatching 345break 346endif 347 348let cleanedLnum = StripEndlineComments ( last_line ) 349 350if cleanedLnum =~ ')' . s: endline 351if BalanceDirection ( cleanedLnum ) <= 0 352call cursor ( lnum , 1 ) 353call searchpos ( ')' . s: endline , 'cW' , lnum ) 354let openedparent = searchpair ( '(' , '' , ')' , 'bW' , 'Skippmatch()' ) 355let cursorPos = col ( "." ) 356if openedparent != lnum 357let lnum = openedparent 358continue 359else 360endif 361else 362let parentArrowPos = -1 363break 364end 365endif 366 367if cleanedLnum =~ '->' 368call cursor ( lnum , cursorPos == -1 ? strwidth ( cleanedLnum ) : cursorPos ) 369let parentArrowPos = searchpos ( '->' , 'cWb' , lnum )[ 1 ] - 1 370 371break 372else 373let parentArrowPos = -1 374break 375endif 376endif 377endwhile 378 379if parentArrowPos == -1 380let parentArrowPos = indent ( lnum ) + shiftwidth () 381end 382 383return parentArrowPos 384endfun "}}} 385 386function ! FindTheIfOfAnElse ( lnum , StopAfterFirstPrevElse ) " {{{ 387 388if getline ( a: lnum ) =~# '^\s*}\s*else\%(if\)\=\>' 389let beforeelse = a: lnum 390else 391let beforeelse = GetLastRealCodeLNum ( a: lnum - 1 ) 392endif 393 394if ! s: level 395let s: iftoskip = 0 396endif 397 398if getline ( beforeelse ) =~# '^\s*\%(}\s*\)\=else\%(\s*if\)\@!\>' 399let s: iftoskip = s: iftoskip + 1 400endif 401 402if getline ( beforeelse ) =~ '^\s*}' 403let beforeelse = FindOpenBracket ( beforeelse , 0 ) 404 405if getline ( beforeelse ) =~ '^\s*{' 406let beforeelse = GetLastRealCodeLNum ( beforeelse - 1 ) 407endif 408endif 409 410 411if ! s: iftoskip && a: StopAfterFirstPrevElse && getline ( beforeelse ) =~# '^\s*\%([}]\s*\)\=else\%(if\)\=\>' 412return beforeelse 413endif 414 415if getline ( beforeelse ) !~# '^\s*if\>' && beforeelse > 1 || s: iftoskip && beforeelse > 1 416 417if s: iftoskip && getline ( beforeelse ) =~# '^\s*if\>' 418let s: iftoskip = s: iftoskip - 1 419endif 420 421let s: level = s: level + 1 422let beforeelse = FindTheIfOfAnElse ( beforeelse , a: StopAfterFirstPrevElse ) 423endif 424 425return beforeelse 426 427endfunction " }}} 428 429let s: defaultORcase = '^\s*\%(default\|case\).*:' 430 431function ! FindTheSwitchIndent ( lnum ) " {{{ 432 433let test = GetLastRealCodeLNum ( a: lnum - 1 ) 434 435if test <= 1 436return indent ( 1 ) - shiftwidth () * b: PHP_vintage_case_default_indent 437end 438 439while getline ( test ) =~ '^\s*}' && test > 1 440let test = GetLastRealCodeLNum ( FindOpenBracket ( test , 0 ) - 1 ) 441 442if getline ( test ) =~ '^\s*switch\>' 443let test = GetLastRealCodeLNum ( test - 1 ) 444endif 445endwhile 446 447if getline ( test ) =~# '^\s*switch\>' 448return indent ( test ) 449elseif getline ( test ) =~# s: defaultORcase 450return indent ( test ) - shiftwidth () * b: PHP_vintage_case_default_indent 451else 452return FindTheSwitchIndent ( test ) 453endif 454 455endfunction "}}} 456 457let s: SynPHPMatchGroups = { 'phpparent' : 1 , 'delimiter' : 1 , 'define' : 1 , 'storageclass' : 1 , 'structure' : 1 , 'exception' : 1 } 458function ! IslinePHP ( lnum , tofind ) " {{{ 459let cline = getline ( a: lnum ) 460 461if a: tofind == "" 462let tofind = "^\\s*[\"'`]*\\s*\\zs\\S" 463else 464let tofind = a: tofind 465endif 466 467let tofind = tofind . '\c' 468 469let coltotest = match ( cline , tofind ) + 1 470 471let synname = synIDattr ( synID ( a: lnum , coltotest , 0 ), "name" ) 472 473if synname ==? 'phpStringSingle' || synname ==? 'phpStringDouble' || synname ==? 'phpBacktick' 474if cline !~ '^\s*[''"`]' " ??? XXX 475return "SpecStringEntrails" 476else 477return synname 478end 479end 480 481if get ( s: SynPHPMatchGroups , tolower ( synname )) || synname =~ '^php' || synname =~? '^javaScript' 482return synname 483else 484return "" 485endif 486endfunction " }}} 487 488let s: autoresetoptions = 0 489if ! s: autoresetoptions 490let s: autoresetoptions = 1 491endif 492 493function ! ResetPhpOptions () 494if ! b: optionsset && & filetype =~ "php" 495if b: PHP_autoformatcomment 496 497setlocal comments = s1:/*,mb:*,ex:*/,://,f:#[,:# 498 499setlocal formatoptions -= t 500setlocal formatoptions += q 501setlocal formatoptions += r 502setlocal formatoptions += o 503setlocal formatoptions += c 504setlocal formatoptions += b 505endif 506let b: optionsset = 1 507endif 508endfunc 509 510call ResetPhpOptions () 511 512function ! GetPhpIndentVersion () 513return "1.75" 514endfun 515 516function ! GetPhpIndent () 517 518let b: GetLastRealCodeLNum_ADD = 0 519 520let UserIsEditing = 0 521if b: PHP_oldchangetick != b: changedtick 522let b: PHP_oldchangetick = b: changedtick 523let UserIsEditing = 1 524endif 525 526if b: PHP_default_indenting 527let b: PHP_default_indenting = g: PHP_default_indenting * shiftwidth () 528endif 529 530let cline = getline ( v: lnum ) 531 532if ! b: PHP_indentinghuge && b: PHP_lastindented > b: PHP_indentbeforelast 533if b: PHP_indentbeforelast 534let b: PHP_indentinghuge = 1 535endif 536let b: PHP_indentbeforelast = b: PHP_lastindented 537endif 538 539if b: InPHPcode_checked && prevnonblank ( v: lnum - 1 ) != b: PHP_lastindented 540if b: PHP_indentinghuge 541let b: PHP_indentinghuge = 0 542let b: PHP_CurrentIndentLevel = b: PHP_default_indenting 543endif 544let real_PHP_lastindented = v: lnum 545let b: PHP_LastIndentedWasComment = 0 546let b: PHP_InsideMultilineComment = 0 547let b: PHP_indentbeforelast = 0 548 549let b: InPHPcode = 0 550let b: InPHPcode_checked = 0 551let b: InPHPcode_and_script = 0 552let b: InPHPcode_tofind = "" 553 554elseif v: lnum > b: PHP_lastindented 555let real_PHP_lastindented = b: PHP_lastindented 556else 557let real_PHP_lastindented = v: lnum 558endif 559 560let b: PHP_lastindented = v: lnum 561 562 563if ! b: InPHPcode_checked " {{{ One time check 564let b: InPHPcode_checked = 1 565let b: UserIsTypingComment = 0 566 567let synname = "" 568if cline !~ '<?.*?>' 569let synname = IslinePHP ( prevnonblank ( v: lnum ), "" ) 570endif 571 572if synname != "" 573if synname ==? "SpecStringEntrails" 574let b: InPHPcode = -1 " thumb down 575let b: InPHPcode_tofind = "" 576elseif synname !=? "phpHereDoc" && synname !=? "phpHereDocDelimiter" 577let b: InPHPcode = 1 578let b: InPHPcode_tofind = "" 579 580if synname =~? '^php\%(Doc\)\?Comment' 581let b: UserIsTypingComment = 1 582let b: InPHPcode_checked = 0 583endif 584 585if synname =~? '^javaScript' 586let b: InPHPcode_and_script = 1 587endif 588 589else 590let b: InPHPcode = 0 591 592let lnum = v: lnum - 1 593while getline ( lnum ) !~? '<<<\s*[''"]\=\a\w*[''"]\=$' && lnum > 1 594let lnum = lnum - 1 595endwhile 596 597let b: InPHPcode_tofind = substitute ( getline ( lnum ), '^.*<<<\s*[''"]\=\(\a\w*\)[''"]\=$' , '^\\s*\1;\\=$' , '' ) 598endif 599else 600let b: InPHPcode = 0 601let b: InPHPcode_tofind = s: PHP_startindenttag 602endif 603endif "!b:InPHPcode_checked }}} 604 605 606" Test if we are indenting PHP code {{{ 607let lnum = prevnonblank ( v: lnum - 1 ) 608let last_line = getline ( lnum ) 609let endline = s: endline 610 611if b: InPHPcode_tofind != "" 612if cline =~? b: InPHPcode_tofind 613let b: InPHPcode_tofind = "" 614let b: UserIsTypingComment = 0 615 616if b: InPHPcode == -1 617let b: InPHPcode = 1 618return -1 619end 620 621let b: InPHPcode = 1 622 623if cline =~ '\*/' 624call cursor ( v: lnum , 1 ) 625if cline !~ '^\*/' 626call search ( '\*/' , 'W' ) 627endif 628let lnum = searchpair ( '/\*' , '' , '\*/' , s: searchpairflags , 'Skippmatch2()' ) 629 630let b: PHP_CurrentIndentLevel = b: PHP_default_indenting 631 632let b: PHP_LastIndentedWasComment = 0 633 634if cline =~ '^\s*\*/' 635return indent ( lnum ) + 1 636else 637return indent ( lnum ) 638endif 639 640elseif cline =~? '<script\>' 641let b: InPHPcode_and_script = 1 642let b: GetLastRealCodeLNum_ADD = v: lnum 643endif 644endif 645endif 646 647if 1 == b: InPHPcode 648 649if ! b: InPHPcode_and_script && last_line =~ '\%(<?.*\)\@<!?>\%(.*<?\)\@!' && IslinePHP ( lnum , '?>' ) =~? "Delimiter" 650if cline !~? s: PHP_startindenttag 651let b: InPHPcode = 0 652let b: InPHPcode_tofind = s: PHP_startindenttag 653elseif cline =~? '<script\>' 654let b: InPHPcode_and_script = 1 655endif 656 657elseif last_line =~ '^[^''"`]\+[''"`]$' && last_line !~ '^\s*\%(//\|#\[\@!\|/\*.*\*/\s*$\)' " a string identifier with nothing after it and no other string identifier before 658let b: InPHPcode = -1 659let b: InPHPcode_tofind = substitute ( last_line , '^.*\([''"`]\).*$' , '^[^\1]*\1[;,]$' , '' ) 660elseif last_line =~? '<<<\s*[''"]\=\a\w*[''"]\=$' 661let b: InPHPcode = 0 662let b: InPHPcode_tofind = substitute ( last_line , '^.*<<<\s*[''"]\=\(\a\w*\)[''"]\=$' , '^\\s*\1;\\=$' , '' ) 663 664elseif ! UserIsEditing && cline =~ '^\s*/\*\%(.*\*/\)\@!' && getline ( v: lnum + 1 ) !~ '^\s*\*' 665let b: InPHPcode = 0 666let b: InPHPcode_tofind = '\*/' 667 668elseif cline =~? '^\s*</script>' 669let b: InPHPcode = 0 670let b: InPHPcode_tofind = s: PHP_startindenttag 671endif 672endif " }}} 673 674 675if 1 > b: InPHPcode && ! b: InPHPcode_and_script 676return -1 677endif 678 679" Indent successive // or # comment the same way the first is {{{ 680let addSpecial = 0 681if cline =~ '^\s*\%(//\|#\[\@!\|/\*.*\*/\s*$\)' 682let addSpecial = b: PHP_outdentSLComments 683if b: PHP_LastIndentedWasComment == 1 684return indent ( real_PHP_lastindented ) 685endif 686let b: PHP_LastIndentedWasComment = 1 687else 688let b: PHP_LastIndentedWasComment = 0 689endif " }}} 690 691" Indent multiline /* comments correctly {{{ 692 693if b: PHP_InsideMultilineComment || b: UserIsTypingComment 694if cline =~ '^\s*\*\%(\/\)\@!' 695if last_line =~ '^\s*/\*' 696return indent ( lnum ) + 1 697else 698return indent ( lnum ) 699endif 700else 701let b: PHP_InsideMultilineComment = 0 702endif 703endif 704 705if ! b: PHP_InsideMultilineComment && cline =~ '^\s*/\*\%(.*\*/\)\@!' 706if getline ( v: lnum + 1 ) !~ '^\s*\*' 707return -1 708endif 709let b: PHP_InsideMultilineComment = 1 710endif " }}} 711 712 713" Things always indented at col 1 (PHP delimiter: <?, ?>, Heredoc end) {{{ 714if cline =~# '^\s*<?' && cline !~ '?>' && b: PHP_outdentphpescape 715return 0 716endif 717 718if cline =~ '^\s*?>' && cline !~# '<?' && b: PHP_outdentphpescape 719return 0 720endif 721 722if ( cline =~? '^\s*\a\w*;$\|^\a\w*$' || ( cline =~? '^\s*[''"`][;,]' && IslinePHP ( v: lnum , '[;,]' ) !~? '^\(phpString[SD]\|phpBacktick\)' ) ) && cline !~? s: notPhpHereDoc 723return 0 724endif " }}} 725 726let s: level = 0 727 728let lnum = GetLastRealCodeLNum ( v: lnum - 1 ) 729 730let last_line = getline ( lnum ) 731let ind = indent ( lnum ) 732 733if ind == 0 && b: PHP_default_indenting 734let ind = b: PHP_default_indenting 735endif 736 737if lnum == 0 738return b: PHP_default_indenting + addSpecial 739endif 740 741 742if cline =~ '^\s*}\%(}}\)\@!' 743let ind = indent ( FindOpenBracket ( v: lnum , 1 )) 744let b: PHP_CurrentIndentLevel = b: PHP_default_indenting 745if b: PHP_BracesAtCodeLevel 746let ind = ind + shiftwidth () 747endif 748return ind 749endif 750 751if cline =~ '^\s*\*/' 752call cursor ( v: lnum , 1 ) 753if cline !~ '^\*/' 754call search ( '\*/' , 'W' ) 755endif 756let lnum = searchpair ( '/\*' , '' , '\*/' , s: searchpairflags , 'Skippmatch2()' ) 757 758let b: PHP_CurrentIndentLevel = b: PHP_default_indenting 759 760if cline =~ '^\s*\*/' 761return indent ( lnum ) + 1 762else 763return indent ( lnum ) 764endif 765endif 766 767 768if last_line =~ '[;}]' . endline && last_line !~ '^[)\]]' && last_line !~# s: defaultORcase && last_line !~ '^\s*[''"`][;,]' 769if ind == b: PHP_default_indenting 770return b: PHP_default_indenting + addSpecial 771elseif b: PHP_indentinghuge && ind == b: PHP_CurrentIndentLevel && cline !~# '^\s*\%(else\|\%(case\|default\).*:\|[})];\=\)' && last_line !~# '^\s*\%(\%(}\s*\)\=else\)\|^\(\s*\S\+\s*\)\+}' . endline && getline ( GetLastRealCodeLNum ( lnum - 1 )) =~ ';' . endline 772return b: PHP_CurrentIndentLevel + addSpecial 773endif 774endif 775 776let LastLineClosed = 0 777 778let terminated = s: terminated 779 780let unstated = s: unstated 781 782 783if ind != b: PHP_default_indenting && cline =~# '^\s*else\%(if\)\=\>' 784let b: PHP_CurrentIndentLevel = b: PHP_default_indenting 785return indent ( FindTheIfOfAnElse ( v: lnum , 1 )) 786elseif cline =~# s: defaultORcase 787return FindTheSwitchIndent ( v: lnum ) + shiftwidth () * b: PHP_vintage_case_default_indent 788elseif cline =~ '^\s*)\=\s*{' 789let previous_line = last_line 790let last_line_num = lnum 791 792while last_line_num > 1 793 794if previous_line =~ terminated || previous_line =~ s: structureHead 795 796let ind = indent ( last_line_num ) 797 798if b: PHP_BracesAtCodeLevel 799let ind = ind + shiftwidth () 800endif 801 802return ind 803endif 804 805let last_line_num = GetLastRealCodeLNum ( last_line_num - 1 ) 806let previous_line = getline ( last_line_num ) 807endwhile 808elseif cline =~ '^\s*->' 809return FindArrowIndent ( lnum ) 810elseif last_line =~# unstated && cline !~ '^\s*);\=' . endline 811let ind = ind + shiftwidth () " we indent one level further when the preceding line is not stated 812return ind + addSpecial 813 814elseif ( ind != b: PHP_default_indenting || last_line =~ '^[)\]]' ) && last_line =~ terminated 815let previous_line = last_line 816let last_line_num = lnum 817let LastLineClosed = 1 818 819let isSingleLineBlock = 0 820while 1 821if ! isSingleLineBlock && previous_line =~ '^\s*}\|;\s*}' . endline 822 823call cursor ( last_line_num , 1 ) 824if previous_line !~ '^}' 825call search ( '}\|;\s*}' . endline , 'W' ) 826end 827let oldLastLine = last_line_num 828let last_line_num = searchpair ( '{' , '' , '}' , 'bW' , 'Skippmatch()' ) 829 830if getline ( last_line_num ) =~ '^\s*{' 831let last_line_num = GetLastRealCodeLNum ( last_line_num - 1 ) 832elseif oldLastLine == last_line_num 833let isSingleLineBlock = 1 834continue 835endif 836 837let previous_line = getline ( last_line_num ) 838 839continue 840else 841 842if getline ( last_line_num ) =~# '^\s*else\%(if\)\=\>' && ! isSingleLineBlock 843let last_line_num = FindTheIfOfAnElse ( last_line_num , 0 ) 844let isSingleLineBlock = 0 845continue 846endif 847 848let isSingleLineBlock = 0 849 850 851let last_match = last_line_num 852 853let one_ahead_indent = indent ( last_line_num ) 854let last_line_num = GetLastRealCodeLNum ( last_line_num - 1 ) 855let two_ahead_indent = indent ( last_line_num ) 856let after_previous_line = previous_line 857let previous_line = getline ( last_line_num ) 858 859 860if previous_line =~# s: defaultORcase . '\|{' . endline 861break 862endif 863 864if after_previous_line =~# '^\s*' . s: blockstart . '.*)' . endline && previous_line =~# '[;}]' . endline 865break 866endif 867 868if one_ahead_indent == two_ahead_indent || last_line_num < 1 869if previous_line =~# '\%(;\|^\s*}\)' . endline || last_line_num < 1 870break 871endif 872endif 873endif 874endwhile 875 876if indent ( last_match ) != ind 877let ind = indent ( last_match ) 878let b: PHP_CurrentIndentLevel = b: PHP_default_indenting 879 880return ind + addSpecial 881endif 882endif 883 884if ( last_line !~ '^\s*}\%(}}\)\@!' ) 885let plinnum = GetLastRealCodeLNum ( lnum - 1 ) 886else 887let plinnum = GetLastRealCodeLNum ( FindOpenBracket ( lnum , 1 ) - 1 ) 888endif 889 890let AntepenultimateLine = getline ( plinnum ) 891 892let last_line = StripEndlineComments ( last_line ) 893 894if ind == b: PHP_default_indenting 895if last_line =~ terminated && last_line !~# s: defaultORcase 896let LastLineClosed = 1 897endif 898endif 899 900if ! LastLineClosed 901 902let openedparent = -1 903 904 905if last_line =~# '[{(\[]' . endline || last_line =~? '\h\w*\s*(.*,$' && AntepenultimateLine !~ '[,(\[]' . endline && BalanceDirection ( last_line ) > 0 906 907let dontIndent = 0 908if last_line =~ '\S\+\s*{' . endline && last_line !~ '^\s*[)\]]\+\(\s*:\s*' . s: PHP_validVariable . '\)\=\s*{' . endline && last_line !~ s: structureHead 909let dontIndent = 1 910endif 911 912if ! dontIndent && ( ! b: PHP_BracesAtCodeLevel || last_line !~# '^\s*{' ) 913let ind = ind + shiftwidth () 914endif 915 916if b: PHP_IndentFunctionCallParameters && last_line =~ s: multilineFunctionCall && last_line !~ s: structureHead && last_line !~ s: arrayDecl 917let ind = ind + b: PHP_IndentFunctionCallParameters * shiftwidth () 918endif 919 920if b: PHP_IndentFunctionDeclarationParameters && last_line =~ s: multilineFunctionDecl 921let ind = ind + b: PHP_IndentFunctionDeclarationParameters * shiftwidth () 922endif 923 924if b: PHP_BracesAtCodeLevel || b: PHP_vintage_case_default_indent == 1 925let b: PHP_CurrentIndentLevel = ind 926 927endif 928 929elseif last_line =~ '),' . endline && BalanceDirection ( last_line ) < 0 930call cursor ( lnum , 1 ) 931call searchpos ( '),' . endline , 'cW' ) 932let openedparent = searchpair ( '(' , '' , ')' , 'bW' , 'Skippmatch()' ) 933if openedparent != lnum 934let ind = indent ( openedparent ) 935endif 936 937elseif last_line =~ s: structureHead 938let ind = ind + shiftwidth () 939 940 941elseif AntepenultimateLine =~ '{' . endline && AntepenultimateLine !~? '^\s*use\>' && AntepenultimateLine !~? s: matchStart || AntepenultimateLine =~ terminated || AntepenultimateLine =~# s: defaultORcase 942let ind = ind + shiftwidth () 943endif 944 945 946if openedparent >= 0 947let last_line = StripEndlineComments ( getline ( openedparent )) 948endif 949endif 950 951if cline =~ '^\s*[)\]];\=' 952call cursor ( v: lnum , 1 ) 953call searchpos ( '[)\]]' , 'cW' ) 954let matchedBlockChar = cline [ col ( '.' ) - 1 ] 955let openedparent = searchpair ( '\M' . s: blockCharsLUT [ matchedBlockChar ], '' , '\M' . matchedBlockChar , 'bW' , 'Skippmatch()' ) 956if openedparent != v: lnum 957let ind = indent ( openedparent ) 958endif 959 960elseif last_line =~ '^\s*->' && last_line !~? s: structureHead && BalanceDirection ( last_line ) <= 0 961let ind = ind - shiftwidth () 962endif 963 964let b: PHP_CurrentIndentLevel = ind 965return ind + addSpecial 966endfunction