Take &verbose variable into account. Fix #48.

This commit is contained in:
Chiel92 2015-04-09 10:59:54 +02:00
parent 84f8c1864f
commit c16a053519

View File

@ -1,17 +1,20 @@
"Function for finding and setting the formatter with the given name " Function for finding and setting the formatter with the given name
function! s:set_formatprg(...) function! s:set_formatprg(...)
let type = a:0 ? a:1 : &filetype let type = a:0 ? a:1 : &filetype
"Support composite filetypes by replacing dots with underscores " Support composite filetypes by replacing dots with underscores
let type = substitute(type, "[.]", "_", "g") let type = substitute(type, "[.]", "_", "g")
"Get formatprg config " Detect verbosity
let s:verbose = &verbose || exists("g:autoformat_verbosemode")
" Get formatprg config
let s:formatprg_var = "g:formatprg_".type let s:formatprg_var = "g:formatprg_".type
let s:formatprg_args_var = "g:formatprg_args_".type let s:formatprg_args_var = "g:formatprg_args_".type
let s:formatprg_args_expr_var = "g:formatprg_args_expr_".type let s:formatprg_args_expr_var = "g:formatprg_args_expr_".type
if !exists(s:formatprg_var) if !exists(s:formatprg_var)
"No formatprg defined " No formatprg defined
if exists("g:autoformat_verbosemode") if s:verbose
echoerr "No formatter defined for filetype '".type."'." echoerr "No formatter defined for filetype '".type."'."
endif endif
return 0 return 0
@ -19,16 +22,16 @@ function! s:set_formatprg(...)
let s:formatprg = eval(s:formatprg_var) let s:formatprg = eval(s:formatprg_var)
let s:formatprg_args = "" let s:formatprg_args = ""
if exists(s:formatprg_args_expr_var) if exists(s:formatprg_args_expr_var)
let s:formatprg_args = eval(eval(s:formatprg_args_expr_var)) let s:formatprg_args = eval(eval(s:formatprg_args_expr_var))
elseif exists(s:formatprg_args_var) elseif exists(s:formatprg_args_var)
let s:formatprg_args = eval(s:formatprg_args_var) let s:formatprg_args = eval(s:formatprg_args_var)
endif endif
"Set correct formatprg path, if it is installed " Set correct formatprg path, if it is installed
if !executable(s:formatprg) if !executable(s:formatprg)
"Configured formatprg not installed " Configured formatprg not installed
if exists("g:autoformat_verbosemode") if s:verbose
echoerr "Defined formatter ".eval(s:formatprg_var)." is not executable." echoerr "Defined formatter ".eval(s:formatprg_var)." is not executable."
endif endif
return 0 return 0
@ -38,25 +41,25 @@ function! s:set_formatprg(...)
return 1 return 1
endfunction endfunction
"set right formatprg before formatting " Set right formatprg before formatting
noremap <expr> gq <SID>set_formatprg() ? 'gq' : 'gq' noremap <expr> gq <SID>set_formatprg() ? 'gq' : 'gq'
"Function for formatting the entire buffer " Function for formatting the entire buffer
function! s:Autoformat(...) function! s:Autoformat(...)
"Save window state " Save window state
let winview=winsaveview() let winview=winsaveview()
if call('<SID>set_formatprg', a:000) if call('<SID>set_formatprg', a:000)
"Autoformat code " Autoformat code
exe "1,$!".&formatprg exe "1,$!".&formatprg
else else
"Autoindent code " Autoindent code
exe "normal gg=G" exe "normal gg=G"
endif endif
"Recall window state " Recall window state
call winrestview(winview) call winrestview(winview)
endfunction endfunction
"Create a command for formatting the entire buffer " Create a command for formatting the entire buffer
command! -nargs=? -complete=filetype Autoformat call s:Autoformat(<f-args>) command! -nargs=? -complete=filetype Autoformat call s:Autoformat(<f-args>)