vim-autoformat/plugin/autoformat.vim

53 lines
1.5 KiB
VimL
Raw Normal View History

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()
"If a formatprg is specified
if &formatprg!=""
"Save window state
let winview=winsaveview()
"Autoformat code
2013-03-09 11:35:24 -05:00
silent exe "normal gggqG"
"Recall window state
call winrestview(winview)
else
2013-03-09 11:35:24 -05:00
echo "No formatter defined for filetype ".&filetype
endif
2012-09-30 16:11:36 -04:00
endfunction
2012-12-02 09:27:50 -05:00
2013-02-25 06:31:28 -05:00
"Create a command for formatting the entire buffer
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
2013-03-09 11:35:24 -05:00
let s:formatterdir = fnamemodify(expand("<sfile>"), ":p:h:h")."/formatters/"
function! s:set_formatprg()
"Reset previous formatprg
set formatprg=""
"Get formatprg config for current filetype
let s:formatprg_var = "g:formatprg_".&filetype
let s:formatprg_args_var = "g:formatprg_args_".&filetype
if !exists(s:formatprg_var) || !exists(s:formatprg_args_var)
"No formatprg configured
return
endif
let s:formatprg = eval(s:formatprg_var)
let s:formatprg_args = eval(s:formatprg_args_var)
"Set correct formatprg path, if it is installed
if !executable(s:formatprg)
let s:formatprg = s:formatterdir.s:formatprg
if !executable(s:formatprg)
"Configured formatprg not installed
return
endif
endif
let b:formatprg = s:formatprg." ".s:formatprg_args
endfunction
"When filetype changes, set correct b:formatprg
au FileType * call s:set_formatprg()
"When current buffer changes, store b:formatprg into &formatprg
au BufEnter * if exists("b:formatprg") | let &formatprg = b:formatprg