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