blob: 208df3ab085b2bbe791584c4677ddfe25b1ff408 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" 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
6" Version: 0.5
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" Changes: 0.5 - fix duplicate indent on open tag, and empty bracketed
15" statements.
16" 0.4 - Fixes for closing php tag, switch statement closure, and php_indent_shortopentags
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.
23if exists("b:did_indent")
24 finish
25endif
26let b:did_indent = 1
27
28setlocal indentexpr=GetPhpIndent()
29"setlocal indentkeys+=0=,0),=EO
30setlocal indentkeys+=0=,0),=EO,=>
31
32" Only define the function once.
33if exists("*GetPhpIndent")
34 finish
35endif
36
37" Handle option(s)
38if exists("php_noindent_switch")
39 let b:php_noindent_switch=1
40endif
41
42function 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
54 " Indent after php open tag
55 if line =~ '<?php'
56 let ind = ind + &sw
57 elseif exists('g:php_indent_shortopentags')
58 " indent after short open tag
59 if line =~ '<?'
60 let ind = ind + &sw
61 endif
62 endif
63 " indent after php closing tag
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
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('{', '', '}',
83 'bW', 'synIDattr(synID(line("."), col("."),
84 0), "name") =~? "string"'))
85 return ind
86 endif
87 " Try to indent switch/case statements as well
88 " Indent blocks enclosed by {} or () or case statements, with some anal requirements
89 if line =~ 'case.*:\|[{(]\s*\(#[^)}]*\)\=$'
90 let ind = ind + &sw
91 " return if the current line is not another case statement of the previous line is a bracket open
92 if cline !~ '.*case.*:\|default:' || line =~ '[{(]\s*\(#[^)}]*\)\=$'
93 return ind
94 endif
95 endif
96 if cline =~ '^\s*case.*:\|^\s*default:\|^\s*[)}]'
97 let ind = ind - &sw
98 " if the last line is a break or return, or the current line is a close bracket,
99 " or if the previous line is a default statement, subtract another
100 if line =~ '^\s*break;\|^\s*return\|' && cline =~ '^\s*[)}]' && pline =~ 'default:'
101 let ind = ind - &sw
102 endif
103 endif
104 " Search the matching bracket (with searchpair()) and set the indent of cline
105 " to the indent of the matching line.
106 if cline =~ '^\s*}'
107 call cursor(line('. '), 1)
108 let ind = indent(searchpair('{', '', '}', 'bW', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string"'))
109 return ind
110 endif
111
112 if line =~ 'default:'
113 let ind = ind + &sw
114 endif
115 return ind
116 endif
117endfunction
118" vim: set ts=4 sw=4: