2015-04-09 04:59:54 -04:00
|
|
|
" Function for finding and setting the formatter with the given name
|
2014-06-29 15:30:05 -04:00
|
|
|
function! s:set_formatprg(...)
|
|
|
|
let type = a:0 ? a:1 : &filetype
|
2015-04-09 04:59:54 -04:00
|
|
|
" Support composite filetypes by replacing dots with underscores
|
2015-01-12 08:17:27 -05:00
|
|
|
let type = substitute(type, "[.]", "_", "g")
|
2014-06-29 15:30:05 -04:00
|
|
|
|
2015-04-09 04:59:54 -04:00
|
|
|
" Detect verbosity
|
|
|
|
let s:verbose = &verbose || exists("g:autoformat_verbosemode")
|
|
|
|
|
|
|
|
" Get formatprg config
|
2014-06-29 15:30:05 -04:00
|
|
|
let s:formatprg_var = "g:formatprg_".type
|
|
|
|
let s:formatprg_args_var = "g:formatprg_args_".type
|
|
|
|
let s:formatprg_args_expr_var = "g:formatprg_args_expr_".type
|
2013-03-10 14:51:39 -04:00
|
|
|
|
2013-03-14 13:50:31 -04:00
|
|
|
if !exists(s:formatprg_var)
|
2015-04-09 04:59:54 -04:00
|
|
|
" No formatprg defined
|
|
|
|
if s:verbose
|
2014-06-29 15:30:05 -04:00
|
|
|
echoerr "No formatter defined for filetype '".type."'."
|
2013-03-16 05:36:16 -04:00
|
|
|
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)
|
2013-03-10 14:51:39 -04:00
|
|
|
|
|
|
|
let s:formatprg_args = ""
|
2015-04-09 04:59:54 -04:00
|
|
|
if exists(s:formatprg_args_expr_var)
|
2013-03-15 18:12:09 -04:00
|
|
|
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)
|
2013-03-10 14:51:39 -04:00
|
|
|
endif
|
2013-03-14 13:50:31 -04:00
|
|
|
|
2015-04-09 04:59:54 -04:00
|
|
|
" Set correct formatprg path, if it is installed
|
2013-03-14 13:50:31 -04:00
|
|
|
if !executable(s:formatprg)
|
2015-04-09 04:59:54 -04:00
|
|
|
" Configured formatprg not installed
|
|
|
|
if s:verbose
|
2013-12-20 14:34:38 -05:00
|
|
|
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-01 09:06:13 -05:00
|
|
|
|
2013-03-15 18:12:09 -04:00
|
|
|
return 1
|
|
|
|
endfunction
|
2013-03-01 09:06:13 -05:00
|
|
|
|
2015-04-09 04:59:54 -04:00
|
|
|
" Set right formatprg before formatting
|
2013-03-26 18:03:15 -04:00
|
|
|
noremap <expr> gq <SID>set_formatprg() ? 'gq' : 'gq'
|
2013-03-14 13:50:31 -04:00
|
|
|
|
2015-04-09 04:59:54 -04:00
|
|
|
" Function for formatting the entire buffer
|
2014-06-29 15:30:05 -04:00
|
|
|
function! s:Autoformat(...)
|
2015-04-09 04:59:54 -04:00
|
|
|
" Save window state
|
2013-03-15 18:12:09 -04:00
|
|
|
let winview=winsaveview()
|
2013-03-26 18:03:15 -04:00
|
|
|
|
2014-06-29 15:30:05 -04:00
|
|
|
if call('<SID>set_formatprg', a:000)
|
2015-04-09 04:59:54 -04:00
|
|
|
" Autoformat code
|
2013-03-26 18:03:15 -04:00
|
|
|
exe "1,$!".&formatprg
|
|
|
|
else
|
2015-04-09 04:59:54 -04:00
|
|
|
" Autoindent code
|
2013-03-26 18:03:15 -04:00
|
|
|
exe "normal gg=G"
|
|
|
|
endif
|
|
|
|
|
2015-04-09 04:59:54 -04:00
|
|
|
" Recall window state
|
2013-03-15 18:12:09 -04:00
|
|
|
call winrestview(winview)
|
2013-03-14 13:50:31 -04:00
|
|
|
endfunction
|
|
|
|
|
2015-04-09 04:59:54 -04:00
|
|
|
" Create a command for formatting the entire buffer
|
2014-06-29 15:30:05 -04:00
|
|
|
command! -nargs=? -complete=filetype Autoformat call s:Autoformat(<f-args>)
|