vim-autoformat/plugin/autoformat.vim

59 lines
1.7 KiB
VimL
Raw Normal View History

2013-12-20 14:34:38 -05:00
"Function for finding and setting the formatter with the given name
2013-03-15 18:12:09 -04:00
function! s:set_formatprg()
2013-03-14 13:50:31 -04:00
"Get formatprg config for current filetype
let s:formatprg_var = "g:formatprg_".&filetype
let s:formatprg_args_var = "g:formatprg_args_".&filetype
let s:formatprg_args_expr_var = "g:formatprg_args_expr_".&filetype
2013-03-14 13:50:31 -04:00
if !exists(s:formatprg_var)
"No formatprg defined
2013-08-18 09:57:04 -04:00
if exists("g:autoformat_verbosemode")
echoerr "No formatter defined for filetype '".&filetype."'."
endif
2013-03-15 18:12:09 -04:00
return 0
2013-03-14 13:50:31 -04:00
endif
let s:formatprg = eval(s:formatprg_var)
let s:formatprg_args = ""
2013-03-15 18:12:09 -04:00
if exists(s:formatprg_args_expr_var)
let s:formatprg_args = eval(eval(s:formatprg_args_expr_var))
elseif exists(s:formatprg_args_var)
let s:formatprg_args = eval(s:formatprg_args_var)
endif
2013-03-14 13:50:31 -04:00
"Set correct formatprg path, if it is installed
if !executable(s:formatprg)
2013-12-20 14:34:38 -05:00
"Configured formatprg not installed
if exists("g:autoformat_verbosemode")
echoerr "Defined formatter ".eval(s:formatprg_var)." is not executable."
2013-03-14 13:50:31 -04:00
endif
2013-12-20 14:34:38 -05:00
return 0
2013-03-14 13:50:31 -04:00
endif
let &formatprg = s:formatprg." ".s:formatprg_args
2013-03-15 18:12:09 -04:00
return 1
endfunction
"set right formatprg before formatting
noremap <expr> gq <SID>set_formatprg() ? 'gq' : 'gq'
2013-03-14 13:50:31 -04:00
"Function for formatting the entire buffer
function! s:Autoformat()
2013-03-15 18:12:09 -04:00
"Save window state
let winview=winsaveview()
if <SID>set_formatprg()
"Autoformat code
exe "1,$!".&formatprg
else
"Autoindent code
exe "normal gg=G"
endif
2013-03-15 18:12:09 -04:00
"Recall window state
call winrestview(winview)
2013-03-14 13:50:31 -04:00
endfunction
"Create a command for formatting the entire buffer
command! Autoformat call s:Autoformat()