2012-09-30 22:11:36 +02:00
|
|
|
"Function for formatting the entire buffer
|
2012-12-02 15:27:50 +01:00
|
|
|
function! s:Autoformat()
|
2012-12-01 14:06:28 +01: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 22:11:36 +02:00
|
|
|
endfunction
|
2012-12-02 15:27:50 +01:00
|
|
|
|
2013-02-25 12:31:28 +01:00
|
|
|
"Create a command for formatting the entire buffer
|
2012-12-02 15:33:03 +01:00
|
|
|
command! Autoformat call s:Autoformat()
|
2012-12-02 17:12:49 +01:00
|
|
|
|
2013-02-08 20:05:52 +01:00
|
|
|
"formatprg is a global option
|
2013-02-08 20:58:43 +01:00
|
|
|
"So when buffer/window/tab changes,
|
|
|
|
"(re)load formatprg from the bufferlocal variable
|
|
|
|
au BufEnter,WinEnter * if exists("b:formatprg") | let &formatprg=b:formatprg
|
2013-02-26 18:21:29 +01: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:formatprgvarname = "g:formatprg_".&filetype
|
|
|
|
if !exists(s:formatprgvarname)
|
|
|
|
echo "No formatter set for this filetype"
|
|
|
|
finish
|
|
|
|
endif
|
|
|
|
s:formatprg = eval(s:formatprgvarname)
|
|
|
|
s:formatprgname = matchstr(s:formatprg, '^[^ ]+')
|
|
|
|
|
|
|
|
"Check if given formatprg is installed
|
|
|
|
if executable(s:formatprgname)
|
|
|
|
b:formatprg = s:formatprg
|
|
|
|
else
|
|
|
|
let s:formatterdir = fnamemodify(expand("<sfile>"), ":h:h")."/formatters/"
|
|
|
|
let s:formatprgname = s:formatterdir.s:formatprgname
|
|
|
|
if executable(s:formatprgname)
|
|
|
|
b:formatprg = s:formatprg
|
|
|
|
endif
|
|
|
|
endif
|