Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | " Vim indent file |
| 2 | " Language: PHP |
| 3 | " Author: Miles Lott <milos@groupwhere.org> |
| 4 | " URL: http://milosch.dyndns.org/php.vim |
Bram Moolenaar | 6bb6836 | 2005-03-22 23:03:44 +0000 | [diff] [blame] | 5 | " Last Change: 2005 Mar 21 |
| 6 | " Version: 0.6 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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 | " |
Bram Moolenaar | 6bb6836 | 2005-03-22 23:03:44 +0000 | [diff] [blame] | 14 | " Changes: 0.6 - fix indention for closing bracket (patch from pierre.habouzit@m4x.org) |
| 15 | " 0.5 - fix duplicate indent on open tag, and empty bracketed statements. |
| 16 | " 0.4 - Fixes for closing php tag, switch statement closure, and php_indent_shortopentags |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 17 | " option from Steffen Bruentjen <vim@kontraphon.de> |
| 18 | " |
| 19 | " Options: php_noindent_switch=1 -- do not try to indent switch/case statements (version 0.1 behavior) |
| 20 | " php_indent_shortopentags=1 -- indent after short php open tags, too |
| 21 | |
| 22 | " Only load this indent file when no other was loaded. |
| 23 | if exists("b:did_indent") |
| 24 | finish |
| 25 | endif |
| 26 | let b:did_indent = 1 |
| 27 | |
| 28 | setlocal indentexpr=GetPhpIndent() |
| 29 | "setlocal indentkeys+=0=,0),=EO |
| 30 | setlocal indentkeys+=0=,0),=EO,=> |
| 31 | |
| 32 | " Only define the function once. |
| 33 | if exists("*GetPhpIndent") |
| 34 | finish |
| 35 | endif |
| 36 | |
| 37 | " Handle option(s) |
| 38 | if exists("php_noindent_switch") |
| 39 | let b:php_noindent_switch=1 |
| 40 | endif |
| 41 | |
| 42 | function GetPhpIndent() |
| 43 | " Find a non-blank line above the current line. |
| 44 | let lnum = prevnonblank(v:lnum - 1) |
| 45 | " Hit the start of the file, use zero indent. |
| 46 | if lnum == 0 |
| 47 | return 0 |
| 48 | endif |
| 49 | let line = getline(lnum) " last line |
| 50 | let cline = getline(v:lnum) " current line |
| 51 | let pline = getline(lnum - 1) " previous to last line |
| 52 | let ind = indent(lnum) |
| 53 | |
Bram Moolenaar | 6bb6836 | 2005-03-22 23:03:44 +0000 | [diff] [blame] | 54 | " Indent after php open tag |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 55 | if line =~ '<?php' |
| 56 | let ind = ind + &sw |
Bram Moolenaar | 6bb6836 | 2005-03-22 23:03:44 +0000 | [diff] [blame] | 57 | elseif exists('g:php_indent_shortopentags') |
| 58 | " indent after short open tag |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 59 | if line =~ '<?' |
| 60 | let ind = ind + &sw |
| 61 | endif |
| 62 | endif |
Bram Moolenaar | 6bb6836 | 2005-03-22 23:03:44 +0000 | [diff] [blame] | 63 | " indent after php closing tag |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 64 | if cline =~ '\M?>' |
| 65 | let ind = ind - &sw |
| 66 | endif |
| 67 | |
| 68 | if exists("b:php_noindent_switch") " version 1 behavior, diy switch/case,etc |
| 69 | " Indent blocks enclosed by {} or () |
| 70 | if line =~ '[{(]\s*\(#[^)}]*\)\=$' |
| 71 | let ind = ind + &sw |
| 72 | endif |
| 73 | if cline =~ '^\s*[)}]' |
| 74 | let ind = ind - &sw |
| 75 | endif |
| 76 | return ind |
Bram Moolenaar | 6bb6836 | 2005-03-22 23:03:44 +0000 | [diff] [blame] | 77 | else |
| 78 | " Search the matching bracket (with searchpair()) and set the indent of |
| 79 | " to the indent of the matching line. |
| 80 | if cline =~ '^\s*}' |
| 81 | call cursor(line('.'), 1) |
| 82 | let ind = indent(searchpair('{', '', '}','bW', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string"')) |
| 83 | return ind |
| 84 | endif |
| 85 | " Try to indent switch/case statements as well |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 86 | " Indent blocks enclosed by {} or () or case statements, with some anal requirements |
| 87 | if line =~ 'case.*:\|[{(]\s*\(#[^)}]*\)\=$' |
| 88 | let ind = ind + &sw |
| 89 | " return if the current line is not another case statement of the previous line is a bracket open |
| 90 | if cline !~ '.*case.*:\|default:' || line =~ '[{(]\s*\(#[^)}]*\)\=$' |
| 91 | return ind |
| 92 | endif |
| 93 | endif |
| 94 | if cline =~ '^\s*case.*:\|^\s*default:\|^\s*[)}]' |
| 95 | let ind = ind - &sw |
| 96 | " if the last line is a break or return, or the current line is a close bracket, |
| 97 | " or if the previous line is a default statement, subtract another |
| 98 | if line =~ '^\s*break;\|^\s*return\|' && cline =~ '^\s*[)}]' && pline =~ 'default:' |
| 99 | let ind = ind - &sw |
| 100 | endif |
| 101 | endif |
| 102 | " Search the matching bracket (with searchpair()) and set the indent of cline |
| 103 | " to the indent of the matching line. |
| 104 | if cline =~ '^\s*}' |
Bram Moolenaar | 6bb6836 | 2005-03-22 23:03:44 +0000 | [diff] [blame] | 105 | call cursor(line('. '), 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 106 | let ind = indent(searchpair('{', '', '}', 'bW', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string"')) |
| 107 | return ind |
| 108 | endif |
| 109 | |
| 110 | if line =~ 'default:' |
| 111 | let ind = ind + &sw |
| 112 | endif |
| 113 | return ind |
| 114 | endif |
| 115 | endfunction |
| 116 | " vim: set ts=4 sw=4: |