2012-09-30 16:11:36 -04:00
|
|
|
"Function for formatting the entire buffer
|
2012-12-02 09:27:50 -05:00
|
|
|
function! s:Autoformat()
|
2012-12-01 08:06:28 -05:00
|
|
|
"If a formatprg is specified
|
|
|
|
if &formatprg!=""
|
|
|
|
"Save window state
|
|
|
|
let winview=winsaveview()
|
|
|
|
"Autoformat code
|
|
|
|
:silent exe "normal gggqG"
|
|
|
|
"Recall window state
|
|
|
|
call winrestview(winview)
|
|
|
|
else
|
|
|
|
echo "No formatter installed for this filetype"
|
|
|
|
endif
|
2012-09-30 16:11:36 -04:00
|
|
|
endfunction
|
2012-12-02 09:27:50 -05:00
|
|
|
|
|
|
|
"Create a command for this
|
2012-12-02 09:33:03 -05:00
|
|
|
command! Autoformat call s:Autoformat()
|
2012-12-02 11:12:49 -05:00
|
|
|
|
|
|
|
"Function for finding and setting the formatter
|
|
|
|
"with the given name, if the formatter is installed
|
|
|
|
"globally or in the formatters folder
|
|
|
|
let s:formatterdir = fnamemodify(expand("<sfile>"), ":h:h")."/formatters/"
|
2012-12-02 13:55:32 -05:00
|
|
|
function! g:FindFormatter(name, args)
|
2012-12-02 11:12:49 -05:00
|
|
|
if executable(a:name)
|
|
|
|
let s:prgpath = a:name
|
|
|
|
else
|
|
|
|
let s:bars = expand("<sfile>")
|
|
|
|
let s:prgpath = s:formatterdir.a:name
|
|
|
|
endif
|
|
|
|
|
|
|
|
if executable(s:prgpath)
|
2013-02-08 14:05:52 -05:00
|
|
|
let b:formatprg=s:prgpath." ".a:args
|
2012-12-02 11:12:49 -05:00
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2013-02-08 14:05:52 -05:00
|
|
|
"formatprg is a global option
|
|
|
|
"So when buffer/window/tab changes, (re)load formatprg from the bufferlocal
|
|
|
|
"variable
|
|
|
|
au BufEnter,WinEnter * let &formatprg=b:formatprg
|
|
|
|
"Default value for b:formatprg is empty string
|
|
|
|
let b:formatprg = ""
|