comparison runtime/indent/php.vim @ 1668:0b796e045c42 v7.2b.000

updated for version 7.2b-000
author vimboss
date Sun, 13 Jul 2008 17:41:49 +0000
parents b9740fb41986
children 7bc41231fbc7
comparison
equal deleted inserted replaced
1667:131dbd3d2a4b 1668:0b796e045c42
1 " Vim indent file 1 " Vim indent file
2 " Language: PHP 2 " Language: PHP
3 " Author: John Wellesz <John.wellesz (AT) teaser (DOT) fr> 3 " Author: John Wellesz <John.wellesz (AT) teaser (DOT) fr>
4 " URL: http://www.2072productions.com/vim/indent/php.vim 4 " URL: http://www.2072productions.com/vim/indent/php.vim
5 " Last Change: 2007 Jun 24 5 " Last Change: 2008 June 7th
6 " Newsletter: http://www.2072productions.com/?to=php-indent-for-vim-newsletter.php 6 " Newsletter: http://www.2072productions.com/?to=php-indent-for-vim-newsletter.php
7 " Version: 1.24 7 " Version: 1.28
8 " 8 "
9 " The change log and all the comments have been removed from this file. 9 " If you find a bug, please e-mail me at John.wellesz (AT) teaser (DOT) fr
10 " with an example of code that breaks the algorithm.
11 "
12 " ---> The change log and all the comments have been removed from this file.
10 " 13 "
11 " For a complete change log and fully commented code, download the script on 14 " For a complete change log and fully commented code, download the script on
12 " 2072productions.com at the URI provided above. 15 " 2072productions.com at the URI provided above.
13 " 16 "
14 " If you find a bug, please e-mail me at John.wellesz (AT) teaser (DOT) fr 17 " If you find a bug, please e-mail me at John.wellesz (AT) teaser (DOT) fr
17 " 20 "
18 " Thanks a lot for using this script. 21 " Thanks a lot for using this script.
19 " 22 "
20 " 23 "
21 " NOTE: This script must be used with PHP syntax ON and with the php syntax 24 " NOTE: This script must be used with PHP syntax ON and with the php syntax
22 " script by Lutz Eymers ( http://www.isp.de/data/php.vim ) that's the script bundled with Vim. 25 " script by Lutz Eymers (http://www.isp.de/data/php.vim ) or with the
23 " 26 " script by Peter Hodge (http://www.vim.org/scripts/script.php?script_id=1571 )
24 " 27 " the later is bunbdled by default with vim 7.
25 " In the case you have syntax errors in your script such as end of HereDoc 28 "
26 " tags not at col 1 you'll have to indent your file 2 times (This script 29 "
27 " will automatically put HereDoc end tags at col 1). 30 " In the case you have syntax errors in your script such as HereDoc end
31 " identifiers not at col 1 you'll have to indent your file 2 times (This
32 " script will automatically put HereDoc end identifiers at col 1 if
33 " they are followed by a ';').
28 " 34 "
29 " 35 "
30 " NOTE: If you are editing file in Unix file format and that (by accident) 36 " NOTE: If you are editing file in Unix file format and that (by accident)
31 " there are '\r' before new lines, this script won't be able to proceed 37 " there are '\r' before new lines, this script won't be able to proceed
32 " correctly and will make many mistakes because it won't be able to match 38 " correctly and will make many mistakes because it won't be able to match
35 " 41 "
36 " :%s /\r$//g 42 " :%s /\r$//g
37 " 43 "
38 " or simply 'let' the option PHP_removeCRwhenUnix to 1 and the script will 44 " or simply 'let' the option PHP_removeCRwhenUnix to 1 and the script will
39 " silently remove them when VIM load this script (at each bufread). 45 " silently remove them when VIM load this script (at each bufread).
40 46 "
41 47 "
42 " Options: PHP_autoformatcomment = 0 to not enable autoformating of comment by 48 " Options: PHP_autoformatcomment = 0 to not enable autoformating of comment by
43 " default, if set to 0, this script will let the 'formatoptions' setting intact. 49 " default, if set to 0, this script will let the 'formatoptions' setting intact.
44 " 50 "
45 " Options: PHP_default_indenting = # of sw (default is 0), # of sw will be 51 " Options: PHP_default_indenting = # of sw (default is 0), # of sw will be
46 " added to the indent of each line of PHP code. 52 " added to the indent of each line of PHP code.
47 " 53 "
48 " Options: PHP_removeCRwhenUnix = 1 to make the script automatically remove CR 54 " Options: PHP_removeCRwhenUnix = 1 to make the script automatically remove CR
49 " at end of lines (by default this option is unset), NOTE that you 55 " at end of lines (by default this option is unset), NOTE that you
50 " MUST remove CR when the fileformat is UNIX else the indentation 56 " MUST remove CR when the fileformat is UNIX else the indentation
51 " won't be correct... 57 " won't be correct!
52 " 58 "
53 " Options: PHP_BracesAtCodeLevel = 1 to indent the '{' and '}' at the same 59 " Options: PHP_BracesAtCodeLevel = 1 to indent the '{' and '}' at the same
54 " level than the code they contain. 60 " level than the code they contain.
55 " Exemple: 61 " Exemple:
56 " Instead of: 62 " Instead of:
65 " foo(); 71 " foo();
66 " } 72 " }
67 " 73 "
68 " NOTE: The script will be a bit slower if you use this option because 74 " NOTE: The script will be a bit slower if you use this option because
69 " some optimizations won't be available. 75 " some optimizations won't be available.
76 "
77 " Options: PHP_vintage_case_default_indent = 1 (defaults to 0) to add a meaningless indent
78 " befaore 'case:' and 'default":' statement in switch block
79 "
70 80
71 if exists("b:did_indent") 81 if exists("b:did_indent")
72 finish 82 finish
73 endif 83 endif
74 let b:did_indent = 1 84 let b:did_indent = 1
87 let b:PHP_BracesAtCodeLevel = PHP_BracesAtCodeLevel 97 let b:PHP_BracesAtCodeLevel = PHP_BracesAtCodeLevel
88 else 98 else
89 let b:PHP_BracesAtCodeLevel = 0 99 let b:PHP_BracesAtCodeLevel = 0
90 endif 100 endif
91 101
102
92 if exists("PHP_autoformatcomment") 103 if exists("PHP_autoformatcomment")
93 let b:PHP_autoformatcomment = PHP_autoformatcomment 104 let b:PHP_autoformatcomment = PHP_autoformatcomment
94 else 105 else
95 let b:PHP_autoformatcomment = 1 106 let b:PHP_autoformatcomment = 1
96 endif 107 endif
108
109 if exists("PHP_vintage_case_default_indent")
110 let b:PHP_vintage_case_default_indent = PHP_vintage_case_default_indent
111 else
112 let b:PHP_vintage_case_default_indent = 0
113 endif
114
115
97 116
98 let b:PHP_lastindented = 0 117 let b:PHP_lastindented = 0
99 let b:PHP_indentbeforelast = 0 118 let b:PHP_indentbeforelast = 0
100 let b:PHP_indentinghuge = 0 119 let b:PHP_indentinghuge = 0
101 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting 120 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting
180 else 199 else
181 break 200 break
182 endif 201 endif
183 202
184 203
185 elseif lastline =~? '^\a\w*;$' && lastline !~? s:notPhpHereDoc 204 elseif lastline =~? '^\a\w*;\=$' && lastline !~? s:notPhpHereDoc " XXX 0607
186 let tofind=substitute( lastline, '\([^;]\+\);', '<<<\1$', '') 205 let tofind=substitute( lastline, '\(\a\w*\);\=', '<<<''\\=\1''\\=$', '') " XXX 0607
187 while getline(lnum) !~? tofind && lnum > 1 206 while getline(lnum) !~? tofind && lnum > 1
188 let lnum = lnum - 1 207 let lnum = lnum - 1
189 endwhile 208 endwhile
190 else 209 else
191 break 210 break
207 226
208 function! Skippmatch2() 227 function! Skippmatch2()
209 228
210 let line = getline(".") 229 let line = getline(".")
211 230
212 if line =~ '\%(".*\)\@<=/\*\%(.*"\)\@=' || line =~ '\%(//.*\)\@<=/\*' 231 if line =~ '\%(".*\)\@<=/\*\%(.*"\)\@=' || line =~ '\%(\%(//\|#\).*\)\@<=/\*'
213 return 1 232 return 1
214 else 233 else
215 return 0 234 return 0
216 endif 235 endif
217 endfun 236 endfun
218 237
219 function! Skippmatch() " {{{ 238 function! Skippmatch() " {{{
220 let synname = synIDattr(synID(line("."), col("."), 0), "name") 239 let synname = synIDattr(synID(line("."), col("."), 0), "name")
221 if synname == "Delimiter" || synname == "phpRegionDelimiter" || synname =~# "^phpParent" || synname == "phpArrayParens" || synname =~# '^php\%(Block\|Brace\)' || synname == "javaScriptBraces" || synname == "phpComment" && b:UserIsTypingComment 240 if synname == "Delimiter" || synname == "phpRegionDelimiter" || synname =~# "^phpParent" || synname == "phpArrayParens" || synname =~# '^php\%(Block\|Brace\)' || synname == "javaScriptBraces" || synname =~# "^phpComment" && b:UserIsTypingComment
222 return 0 241 return 0
223 else 242 else
224 return 1 243 return 1
225 endif 244 endif
226 endfun " }}} 245 endfun " }}}
293 else 312 else
294 return "" 313 return ""
295 endif 314 endif
296 endfunction " }}} 315 endfunction " }}}
297 316
298 let s:notPhpHereDoc = '\%(break\|return\|continue\|exit\);' 317 let s:notPhpHereDoc = '\%(break\|return\|continue\|exit\|else\)'
299 let s:blockstart = '\%(\%(\%(}\s*\)\=else\%(\s\+\)\=\)\=if\>\|else\>\|while\>\|switch\>\|for\%(each\)\=\>\|declare\>\|class\>\|interface\>\|abstract\>\|try\>\|catch\>\|[|&]\)' 318 let s:blockstart = '\%(\%(\%(}\s*\)\=else\%(\s\+\)\=\)\=if\>\|else\>\|while\>\|switch\>\|for\%(each\)\=\>\|declare\>\|class\>\|interface\>\|abstract\>\|try\>\|catch\>\)'
300 319
301 let s:autorestoptions = 0 320 let s:autorestoptions = 0
302 if ! s:autorestoptions 321 if ! s:autorestoptions
303 au BufWinEnter,Syntax *.php,*.php3,*.php4,*.php5 call ResetOptions() 322 au BufWinEnter,Syntax *.php,*.php3,*.php4,*.php5 call ResetOptions()
304 let s:autorestoptions = 1 323 let s:autorestoptions = 1
308 if ! b:optionsset 327 if ! b:optionsset
309 if b:PHP_autoformatcomment 328 if b:PHP_autoformatcomment
310 329
311 setlocal comments=s1:/*,mb:*,ex:*/,://,:# 330 setlocal comments=s1:/*,mb:*,ex:*/,://,:#
312 331
313 " setlocal formatoptions-=t 332 setlocal formatoptions-=t
314 setlocal formatoptions+=q 333 setlocal formatoptions+=q
315 setlocal formatoptions+=r 334 setlocal formatoptions+=r
316 setlocal formatoptions+=o 335 setlocal formatoptions+=o
317 " setlocal formatoptions+=w 336 setlocal formatoptions+=w
318 setlocal formatoptions+=c 337 setlocal formatoptions+=c
319 setlocal formatoptions+=b 338 setlocal formatoptions+=b
320 endif 339 endif
321 let b:optionsset = 1 340 let b:optionsset = 1
322 endif 341 endif
339 let cline = getline(v:lnum) 358 let cline = getline(v:lnum)
340 359
341 if !b:PHP_indentinghuge && b:PHP_lastindented > b:PHP_indentbeforelast 360 if !b:PHP_indentinghuge && b:PHP_lastindented > b:PHP_indentbeforelast
342 if b:PHP_indentbeforelast 361 if b:PHP_indentbeforelast
343 let b:PHP_indentinghuge = 1 362 let b:PHP_indentinghuge = 1
344 echom 'Large indenting detected, speed optimizations engaged' 363 echom 'Large indenting detected, speed optimizations engaged (v1.28)'
345 endif 364 endif
346 let b:PHP_indentbeforelast = b:PHP_lastindented 365 let b:PHP_indentbeforelast = b:PHP_lastindented
347 endif 366 endif
348 367
349 if b:InPHPcode_checked && prevnonblank(v:lnum - 1) != b:PHP_lastindented 368 if b:InPHPcode_checked && prevnonblank(v:lnum - 1) != b:PHP_lastindented
379 if synname!="" 398 if synname!=""
380 if synname != "phpHereDoc" && synname != "phpHereDocDelimiter" 399 if synname != "phpHereDoc" && synname != "phpHereDocDelimiter"
381 let b:InPHPcode = 1 400 let b:InPHPcode = 1
382 let b:InPHPcode_tofind = "" 401 let b:InPHPcode_tofind = ""
383 402
384 if synname == "phpComment" 403 if synname =~# "^phpComment"
385 let b:UserIsTypingComment = 1 404 let b:UserIsTypingComment = 1
386 else 405 else
387 let b:UserIsTypingComment = 0 406 let b:UserIsTypingComment = 0
388 endif 407 endif
389 408
394 else 413 else
395 let b:InPHPcode = 0 414 let b:InPHPcode = 0
396 let b:UserIsTypingComment = 0 415 let b:UserIsTypingComment = 0
397 416
398 let lnum = v:lnum - 1 417 let lnum = v:lnum - 1
399 while getline(lnum) !~? '<<<\a\w*$' && lnum > 1 418 while getline(lnum) !~? '<<<''\=\a\w*''\=$' && lnum > 1
400 let lnum = lnum - 1 419 let lnum = lnum - 1
401 endwhile 420 endwhile
402 421
403 let b:InPHPcode_tofind = substitute( getline(lnum), '^.*<<<\(\a\w*\)\c', '^\\s*\1;$', '') 422 let b:InPHPcode_tofind = substitute( getline(lnum), '^.*<<<''\=\(\a\w*\)''\=$', '^\\s*\1;\\=$', '') " XXX 0607
404 endif 423 endif
405 else 424 else
406 let b:InPHPcode = 0 425 let b:InPHPcode = 0
407 let b:UserIsTypingComment = 0 426 let b:UserIsTypingComment = 0
408 let b:InPHPcode_tofind = '<?\%(.*?>\)\@!\|<script.*>' 427 let b:InPHPcode_tofind = '<?\%(.*?>\)\@!\|<script.*>'
451 let b:InPHPcode_tofind = s:PHP_startindenttag 470 let b:InPHPcode_tofind = s:PHP_startindenttag
452 elseif cline =~? '<script\>' 471 elseif cline =~? '<script\>'
453 let b:InPHPcode_and_script = 1 472 let b:InPHPcode_and_script = 1
454 endif 473 endif
455 474
456 elseif last_line =~? '<<<\a\w*$' 475 elseif last_line =~? '<<<''\=\a\w*''\=$' " XXX 0607
457 let b:InPHPcode = 0 476 let b:InPHPcode = 0
458 let b:InPHPcode_tofind = substitute( last_line, '^.*<<<\(\a\w*\)\c', '^\\s*\1;$', '') 477 let b:InPHPcode_tofind = substitute( last_line, '^.*<<<''\=\(\a\w*\)''\=$', '^\\s*\1;\\=$', '') " XXX 0607
459 478
460 elseif !UserIsEditing && cline =~ '^\s*/\*\%(.*\*/\)\@!' && getline(v:lnum + 1) !~ '^\s*\*' 479 elseif !UserIsEditing && cline =~ '^\s*/\*\%(.*\*/\)\@!' && getline(v:lnum + 1) !~ '^\s*\*'
461 let b:InPHPcode = 0 480 let b:InPHPcode = 0
462 let b:InPHPcode_tofind = '\*/' 481 let b:InPHPcode_tofind = '\*/'
463 482
511 530
512 if cline =~ '^\s*?>' && cline !~# '<?' 531 if cline =~ '^\s*?>' && cline !~# '<?'
513 return 0 532 return 0
514 endif 533 endif
515 534
516 if cline =~? '^\s*\a\w*;$' && cline !~? s:notPhpHereDoc 535 if cline =~? '^\s*\a\w*;$\|^\a\w*$' && cline !~? s:notPhpHereDoc " XXX 0607
517 return 0 536 return 0
518 endif " }}} 537 endif " }}}
519 538
520 let s:level = 0 539 let s:level = 0
521 540
556 endif 575 endif
557 endif 576 endif
558 577
559 let defaultORcase = '^\s*\%(default\|case\).*:' 578 let defaultORcase = '^\s*\%(default\|case\).*:'
560 579
561 if last_line =~ '[;}]'.endline && last_line !~# defaultORcase 580 if last_line =~ '[;}]'.endline && last_line !~ '^)' && last_line !~# defaultORcase " Added && last_line !~ '^)' on 2007-12-30
562 if ind==b:PHP_default_indenting 581 if ind==b:PHP_default_indenting
563 return b:PHP_default_indenting 582 return b:PHP_default_indenting
564 elseif b:PHP_indentinghuge && ind==b:PHP_CurrentIndentLevel && cline !~# '^\s*\%(else\|\%(case\|default\).*:\|[})];\=\)' && last_line !~# '^\s*\%(\%(}\s*\)\=else\)' && getline(GetLastRealCodeLNum(lnum - 1))=~';'.endline 583 elseif b:PHP_indentinghuge && ind==b:PHP_CurrentIndentLevel && cline !~# '^\s*\%(else\|\%(case\|default\).*:\|[})];\=\)' && last_line !~# '^\s*\%(\%(}\s*\)\=else\)' && getline(GetLastRealCodeLNum(lnum - 1))=~';'.endline
565 return b:PHP_CurrentIndentLevel 584 return b:PHP_CurrentIndentLevel
566 endif 585 endif
567 endif 586 endif
568 587
569 let LastLineClosed = 0 588 let LastLineClosed = 0
570 589
571 let terminated = '\%(;\%(\s*?>\)\=\|<<<\a\w*\|}\)'.endline 590 let terminated = '\%(;\%(\s*?>\)\=\|<<<''\=\a\w*''\=$\|^\s*}\)'.endline " XXX 0607
572 591
573 let unstated = '\%(^\s*'.s:blockstart.'.*)\|\%(//.*\)\@<!\<e'.'lse\>\)'.endline 592 let unstated = '\%(^\s*'.s:blockstart.'.*)\|\%(//.*\)\@<!\<e'.'lse\>\)'.endline
574 593
575 if ind != b:PHP_default_indenting && cline =~# '^\s*else\%(if\)\=\>' 594 if ind != b:PHP_default_indenting && cline =~# '^\s*else\%(if\)\=\>'
576 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting 595 let b:PHP_CurrentIndentLevel = b:PHP_default_indenting
577 return indent(FindTheIfOfAnElse(v:lnum, 1)) 596 return indent(FindTheIfOfAnElse(v:lnum, 1))
578 elseif cline =~ '^\s*{' 597 elseif cline =~ '^\s*)\=\s*{'
579 let previous_line = last_line 598 let previous_line = last_line
580 let last_line_num = lnum 599 let last_line_num = lnum
581 600
582 while last_line_num > 1 601 while last_line_num > 1
583 602
584 if previous_line =~ '^\s*\%(' . s:blockstart . '\|\%([a-zA-Z]\s*\)*function\)' && previous_line !~ '^\s*[|&]' 603 if previous_line =~ '^\s*\%(' . s:blockstart . '\|\%([a-zA-Z]\s*\)*function\)'
585 604
586 let ind = indent(last_line_num) 605 let ind = indent(last_line_num)
587 606
588 if b:PHP_BracesAtCodeLevel 607 if b:PHP_BracesAtCodeLevel
589 let ind = ind + &sw 608 let ind = ind + &sw
594 613
595 let last_line_num = last_line_num - 1 614 let last_line_num = last_line_num - 1
596 let previous_line = getline(last_line_num) 615 let previous_line = getline(last_line_num)
597 endwhile 616 endwhile
598 617
599 elseif last_line =~# unstated && cline !~ '^\s*{\|^\s*);\='.endline 618 elseif last_line =~# unstated && cline !~ '^\s*);\='.endline
600 let ind = ind + &sw 619 let ind = ind + &sw " we indent one level further when the preceding line is not stated
601 return ind 620 return ind
602 621
603 elseif ind != b:PHP_default_indenting && last_line =~ terminated 622 elseif (ind != b:PHP_default_indenting || last_line =~ '^)' ) && last_line =~ terminated " Added || last_line =~ '^)' on 2007-12-30 (array indenting [rpblem broke other things)
604 let previous_line = last_line 623 let previous_line = last_line
605 let last_line_num = lnum 624 let last_line_num = lnum
606 let LastLineClosed = 1 625 let LastLineClosed = 1
607 626
608 while 1 627 while 1
640 if after_previous_line=~# '^\s*'.s:blockstart.'.*)'.endline && previous_line =~# '[;}]'.endline 659 if after_previous_line=~# '^\s*'.s:blockstart.'.*)'.endline && previous_line =~# '[;}]'.endline
641 break 660 break
642 endif 661 endif
643 662
644 if one_ahead_indent == two_ahead_indent || last_line_num < 1 663 if one_ahead_indent == two_ahead_indent || last_line_num < 1
645 if previous_line =~# '[;}]'.endline || last_line_num < 1 664 if previous_line =~# '\%(;\|^\s*}\)'.endline || last_line_num < 1
646 break 665 break
647 endif 666 endif
648 endif 667 endif
649 endif 668 endif
650 endwhile 669 endwhile
678 697
679 if !b:PHP_BracesAtCodeLevel || last_line !~# '^\s*{' 698 if !b:PHP_BracesAtCodeLevel || last_line !~# '^\s*{'
680 let ind = ind + &sw 699 let ind = ind + &sw
681 endif 700 endif
682 701
683 if b:PHP_BracesAtCodeLevel || cline !~# defaultORcase 702 if b:PHP_BracesAtCodeLevel || b:PHP_vintage_case_default_indent == 1 || cline !~# defaultORcase
684 let b:PHP_CurrentIndentLevel = ind 703 let b:PHP_CurrentIndentLevel = ind
704
685 return ind 705 return ind
686 endif 706 endif
687 707
688 elseif last_line =~ '\S\+\s*),'.endline 708 elseif last_line =~ '\S\+\s*),'.endline
689 call cursor(lnum, 1) 709 call cursor(lnum, 1)
690 call search('),'.endline, 'W') 710 call search('),'.endline, 'W')
691 let openedparent = searchpair('(', '', ')', 'bW', 'Skippmatch()') 711 let openedparent = searchpair('(', '', ')', 'bW', 'Skippmatch()')
692 if openedparent != lnum 712 if openedparent != lnum
693 let ind = indent(openedparent) 713 let ind = indent(openedparent)
694 endif 714 endif
695 715 elseif last_line =~ '^\s*'.s:blockstart
696
697 elseif cline !~ '^\s*{' && pline =~ '\%(;\%(\s*?>\)\=\|<<<\a\w*\|{\|^\s*'.s:blockstart.'\s*(.*)\)'.endline.'\|^\s*}\|'.defaultORcase
698
699 let ind = ind + &sw 716 let ind = ind + &sw
700 717
701 endif 718 elseif last_line =~# defaultORcase
702 719 let ind = ind + &sw
703 elseif last_line =~# defaultORcase 720
704 let ind = ind + &sw 721
722 elseif pline =~ '\%(;\%(\s*?>\)\=\|<<<''\=\a\w*''\=$\|^\s*}\|{\)'.endline . '\|' . defaultORcase
723
724 let ind = ind + &sw
725 endif
726
705 endif 727 endif
706 728
707 if cline =~ '^\s*);\=' 729 if cline =~ '^\s*);\='
708 let ind = ind - &sw 730 let ind = ind - &sw
709 elseif cline =~# defaultORcase 731 elseif cline =~# defaultORcase && last_line !~# defaultORcase
710 let ind = ind - &sw 732 let ind = ind - &sw
711 733
712 endif 734 endif
713 735
714 let b:PHP_CurrentIndentLevel = ind 736 let b:PHP_CurrentIndentLevel = ind