454abca014baefceaed194cf25256745d73c1814

Author: AD7six

Date: 2009-11-19 22:00:25 +0100

Make FormatPHP public, map to ctrl+F for the whole file in normal mode, and ctrl+F for the selected section in visual mode

diff --git a/vendors/vim/plugins/cakephp.vim b/vendors/vim/plugins/cakephp.vim index cc6cf59..6fb0f80 100644 --- a/vendors/vim/plugins/cakephp.vim +++ b/vendors/vim/plugins/cakephp.vim @@ -165,10 +165,6 @@ endfunction " Function: BufferWritePre() " EOL markers are not desired, change to binary mode before saving so one isn't added function s:BufferWritePre() - "TODO configure this? - if (&ft == 'php') - call s:FormatPhp() - endif " set to binary and remove eol let b:save_bin = &bin let &l:bin = 1 @@ -252,6 +248,39 @@ function Cake(...) endfor endfunction +" Function: FormatPhp() +" (Attempt to) autoformat php code according to various conventions +function FormatPhp() range + " put a blank line before comment blocks + :silent '<,'>s/\([\{\}\/;]\)\n\zs\/\*/\r\/\*/e + " correct doc block headers + :silent '<,'>s/^\s*\/\*\*/\/\*\*/e + " correct doc block contents/tail that is indented ( * or */) + :silent '<,'>s/^\s*\*/ \*/e + + "remove trailing whitespace + :silent '<,'>s/\s\s*$//e + + " auto correct lines which start with a { + :silent '<,'>s/\n\s*{/ {/e + " auto correct lines which start with 'else' + :silent '<,'>s/\n\s*else/ else/e + " add parenthesese to a trailing else or if + :silent '<,'>s/\(else|if\)\s*\n/\1 {\n/e + " add parenthesese to what looks like an else with no parenthesese + :silent '<,'>s/^\(\s*\)\zselse\s*\([^{]\)/else {\n\t\1\2/e + + " correct whitespace around comas (parameters) + :silent '<,'>s/,\(\S\)\@=/, /ge + " correct whitespace around assignments/comparisons x=y becomes x = y + :silent '<,'>s/\([^=<>& ]\)\([!<]\?==\?[=>&]\?\)\([^=<>& ]\)/\1 \2 \3/ge + " correct whitespace around assignments/comparisons x= y becomes x = y + :silent '<,'>s/\([^=<>& ]\)\([!<]\?==\?[=>&]\?\)/\1 \2/ge + " correct whitespace around assignments/comparisons x =y becomes x = y + :silent '<,'>s/\([!<]\?==\?[=>&]\?\)\([^=<>& ]\)/\1 \2/ge + ":silent %s/=\([^=&> ]\)\@=/= /e +endfunction + " Function: Controller() " Open the controller file that is associated with whatever is currently being edited " If the function is known - search for and jump to it @@ -449,7 +478,7 @@ function s:SetupCommands() " command -nargs=0 F %call s:CleanBuffer() "endif if !exists(":R") - command -nargs=0 R call s:FormatPhp() + command -nargs=0 R %call FormatPhp() endif if !exists(":L") command -bar -narg=0 L call DocDebug() @@ -481,7 +510,8 @@ function s:SetupMappings() "if !hasmapto('<Plug>CleanBuffer') " map <buffer> <unique> <Leader>f <Plug>CleanBuffer "endif - nnoremap <buffer> <C-F> :bufdo! :call <SID>FormatComments()<CR> + nnoremap <buffer> <C-F> :%call FormatPhp()<CR> + vnoremap <buffer> <C-F> :call FormatPhp()<CR> nnoremap <buffer> <C-G> :call <SID>CleanBuffer()<CR> inoremap <buffer> <C-P> <Esc>:call DocSingle()<CR>i nnoremap <buffer> <C-P> :call DocSingle()<CR> @@ -521,31 +551,6 @@ function s:ReprocessComments() %:call DocRange() endfunction -function s:FormatPhp() - " put a blank line before comment blocks - :silent %s/\([\{\}\/;]\)\n\zs\/\*/\r\/\*/e - " correct doc block headers - :silent %s/^\s*\/\*\*/\/\*\*/e - " correct doc block contents/tail that is indented ( * or */) - :silent %s/^\s*\*/ \*/e - - "remove trailing whitespace - :silent %s/\s\s*$//e - - " auto correct lines which start with a { - :silent %s/\n\s*{/ {/e - " auto correct lines which start with 'else' - :silent %s/\n\s*else/ else/e - " add parenthesese to a trailing else or if - :silent %s/\(else|if)\s*\n/\1 {\n/e - " add parenthesese to what looks like an else with no parenthesese - :silent %s/^\(\s*\)\zselse\s*\([^{]\)/else {\n\t\1\2/e - - " correct whitespace around comas (parameters) - :silent %s/,\(\S\)\@=/, /e - " correct whitespace around assignments/comparisons - :silent %s/=\([^=&> ]\)\@=/= /e -endfunction " Function: FormatComments() " Correct whitespace around doc blocks function s:FormatComments()