home *** CD-ROM | disk | FTP | other *** search
/ vim.ftp.fu-berlin.de / 2015-02-03.vim.ftp.fu-berlin.de.tar / vim.ftp.fu-berlin.de / unix / vim-6.2.tar.bz2 / vim-6.2.tar / vim62 / runtime / indent / php.vim < prev    next >
Encoding:
Text File  |  2003-05-11  |  2.6 KB  |  89 lines

  1. " Vim indent file
  2. " Language:    Php
  3. " Author:    Miles Lott <milos@groupwhere.org>
  4. " URL:        http://groupwhere.org/php.vim
  5. " Last Change:    2003 May 11
  6. " Version:    0.3
  7. " Notes:  Close all switches with default:\nbreak; and it will look better.
  8. "          Also, open and close brackets should be alone on a line.
  9. "          This is my preference, and the only way this will look nice.
  10. "          Try an older version if you care less about the formatting of
  11. "          switch/case.    It is nearly perfect for anyone regardless of your
  12. "          stance on brackets.
  13. "
  14. " Options    php_noindent_switch=1 -- do not try to indent switch/case statements (version 0.1 behavior)
  15.  
  16. " Only load this indent file when no other was loaded.
  17. if exists("b:did_indent")
  18.     finish
  19. endif
  20. let b:did_indent = 1
  21.  
  22. setlocal indentexpr=GetPhpIndent()
  23. setlocal indentkeys+=0=,0),=EO
  24.  
  25. " Only define the function once.
  26. if exists("*GetPhpIndent")
  27.     finish
  28. endif
  29.  
  30. " Handle option(s)
  31. if exists("php_noindent_switch")
  32.     let b:php_noindent_switch=1
  33. endif
  34.  
  35. function GetPhpIndent()
  36.     " Find a non-blank line above the current line.
  37.     let lnum = prevnonblank(v:lnum - 1)
  38.     " Hit the start of the file, use zero indent.
  39.     if lnum == 0
  40.         return 0
  41.     endif
  42.     let line = getline(lnum)    " last line
  43.     let cline = getline(v:lnum) " current line
  44.     let pline = getline(lnum - 1) " previous to last line
  45.     let ind = indent(lnum)
  46.  
  47.     " Indent after php open tags
  48.     if line =~ '<?php'
  49.         let ind = ind + &sw
  50.     endif
  51.     if cline =~ '\?\>' " Please fix this...
  52.         let ind = ind - &sw
  53.     endif
  54.  
  55.     if exists("b:php_noindent_switch") " version 1 behavior, diy switch/case,etc
  56.         " Indent blocks enclosed by {} or ()
  57.         if line =~ '[{(]\s*\(#[^)}]*\)\=$'
  58.             let ind = ind + &sw
  59.         endif
  60.         if cline =~ '^\s*[)}]'
  61.             let ind = ind - &sw
  62.         endif
  63.         return ind
  64.     else " Try to indent switch/case statements as well
  65.         " Indent blocks enclosed by {} or () or case statements, with some anal requirements
  66.         if line =~ 'case.*:\|[{(]\s*\(#[^)}]*\)\=$'
  67.             let ind = ind + &sw
  68.             " return if the current line is not another case statement of the previous line is a bracket open
  69.             if cline !~ '.*case.*:\|default:' || line =~ '[{(]\s*\(#[^)}]*\)\=$'
  70.                 return ind
  71.             endif
  72.         endif
  73.         if cline =~ '^\s*case.*:\|^\s*default:\|^\s*[)}]'
  74.             let ind = ind - &sw
  75.             " if the last line is a break or return, or the current line is a close bracket,
  76.             " or if the previous line is a default statement, subtract another
  77.             if line =~ '^\s*break;\|^\s*return\|' && cline =~ '^\s*[)}]' && pline =~ 'default:'
  78.                 let ind = ind - &sw
  79.             endif
  80.         endif
  81.  
  82.         if line =~ 'default:'
  83.             let ind = ind + &sw
  84.         endif
  85.         return ind
  86.     endif
  87. endfunction
  88. " vim: set ts=4 sw=4:
  89.