fix in the php vim indent plugin

Miles Lott milos@groupwhere.org
Mon, 21 Mar 2005 08:05:33 -0600


This is a multi-part message in MIME format.
--------------060607060606060408010605
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 8bit

Thanks for the fix!  I tested it here and it works well for me, too.  I 
forwarded a copy to Bram for the next official release, now labeled as 
0.6 with a credit to you.  Ah, the best fixes are the simple ones - 
crazy code-formatting bug...

ttyl

Pierre Habouzit wrote:
> Le Lundi 21 Mars 2005 14:40, Miles Lott a écrit :
> 
>>Which version was this for?  0.3 should be back in the next release
>>due to problems in 0.5.  If this is for 0.5 let me know.
> 
> 
> it is for 0.5
> 
> PS: btw, please keep the Cc: to the pkg-vim*maintainers list (since I'm 
> not the only debian vim packager)
> 
> cheers,

--------------060607060606060408010605
Content-Type: text/plain;
 name="php.vim-0.6"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="php.vim-0.6"

" Vim indent file
" Language:	PHP
" Author:	Miles Lott <milos@groupwhere.org>
" URL:		http://milosch.dyndns.org/php.vim
" Last Change:	2005 March 21
" Version:	0.6
" Notes:  Close all switches with default:\nbreak; and it will look better.
"         Also, open and close brackets should be alone on a line.
"         This is my preference, and the only way this will look nice.
"         Try an older version if you care less about the formatting of
"         switch/case.  It is nearly perfect for anyone regardless of your
"         stance on brackets.
"
" Changes: 0.6 - fix indention for closing bracket (patch from pierre.habouzit@m4x.org)
"          0.5 - fix duplicate indent on open tag, and empty bracketed statements.
"          0.4 - Fixes for closing php tag, switch statement closure, and php_indent_shortopentags
"          option from Steffen Bruentjen <vim@kontraphon.de>
"
" Options: php_noindent_switch=1 -- do not try to indent switch/case statements (version 0.1 behavior)
"          php_indent_shortopentags=1 -- indent after short php open tags, too

" Only load this indent file when no other was loaded.
if exists("b:did_indent")
	finish
endif
let b:did_indent = 1

setlocal indentexpr=GetPhpIndent()
"setlocal indentkeys+=0=,0),=EO
setlocal indentkeys+=0=,0),=EO,=>

" Only define the function once.
if exists("*GetPhpIndent")
	finish
endif

" Handle option(s)
if exists("php_noindent_switch")
	let b:php_noindent_switch=1
endif

function GetPhpIndent()
	" Find a non-blank line above the current line.
	let lnum = prevnonblank(v:lnum - 1)
	" Hit the start of the file, use zero indent.
	if lnum == 0
		return 0
	endif
	let line = getline(lnum)    " last line
	let cline = getline(v:lnum) " current line
	let pline = getline(lnum - 1) " previous to last line
	let ind = indent(lnum)

	" Indent after php open tag
	if line =~ '<?php'
		let ind = ind + &sw
	elseif exists('g:php_indent_shortopentags')
		" indent after short open tag
		if line =~ '<?'
			let ind = ind + &sw
		endif
	endif
	" indent after php closing tag
	if cline =~ '\M?>'
		let ind = ind - &sw
	endif

	if exists("b:php_noindent_switch") " version 1 behavior, diy switch/case,etc
		" Indent blocks enclosed by {} or ()
		if line =~ '[{(]\s*\(#[^)}]*\)\=$'
			let ind = ind + &sw
		endif
		if cline =~ '^\s*[)}]'
			let ind = ind - &sw
		endif
		return ind
	else
		" Search the matching bracket (with searchpair()) and set the indent of
		" to the indent of the matching line.
		if cline =~ '^\s*}'
			call cursor(line('.'), 1)
			let ind = indent(searchpair('{', '', '}','bW', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string"'))
			return ind
		endif
		" Try to indent switch/case statements as well
		" Indent blocks enclosed by {} or () or case statements, with some anal requirements
		if line =~ 'case.*:\|[{(]\s*\(#[^)}]*\)\=$'
			let ind = ind + &sw
			" return if the current line is not another case statement of the previous line is a bracket open
			if cline !~ '.*case.*:\|default:' || line =~ '[{(]\s*\(#[^)}]*\)\=$'
				return ind
			endif
		endif
		if cline =~ '^\s*case.*:\|^\s*default:\|^\s*[)}]'
			let ind = ind - &sw
			" if the last line is a break or return, or the current line is a close bracket,
			" or if the previous line is a default statement, subtract another
			if line =~ '^\s*break;\|^\s*return\|' && cline =~ '^\s*[)}]' && pline =~ 'default:'
				let ind = ind - &sw
			endif
		endif
		" Search the matching bracket (with searchpair()) and set the indent of cline
		" to the indent of the matching line.
		if cline =~ '^\s*}'
			call cursor(line('. '), 1)
			let ind = indent(searchpair('{', '', '}', 'bW', 'synIDattr(synID(line("."), col("."), 0), "name") =~? "string"'))
			return ind
		endif

		if line =~ 'default:'
			let ind = ind + &sw
		endif
		return ind
	endif
endfunction
" vim: set ts=4 sw=4:

--------------060607060606060408010605--