2013-02-26 12:21:29 -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/"
|
2013-03-15 18:12:09 -04:00
|
|
|
function! s:set_formatprg()
|
2013-03-14 13:50:31 -04:00
|
|
|
"Reset previous formatprg
|
|
|
|
set formatprg=""
|
2013-02-26 12:21:29 -05:00
|
|
|
|
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-10 14:51:39 -04:00
|
|
|
|
2013-03-14 13:50:31 -04:00
|
|
|
if !exists(s:formatprg_var)
|
|
|
|
"No formatprg defined
|
2013-03-15 18:12:09 -04:00
|
|
|
echo "No formatter defined for filetype '".&filetype."'."
|
|
|
|
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 = ""
|
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)
|
2013-03-10 14:51:39 -04:00
|
|
|
endif
|
2013-03-14 13:50:31 -04:00
|
|
|
|
|
|
|
"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
|
2013-03-15 18:12:09 -04:00
|
|
|
echo "Defined formatter ".eval(s:formatprg_var)." is not executable."
|
|
|
|
return 0
|
2013-03-14 13:50:31 -04:00
|
|
|
endif
|
|
|
|
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
|
|
|
|
2013-03-14 13:50:31 -04:00
|
|
|
"When gq has been pressed:
|
|
|
|
"1. set right formatprg
|
|
|
|
"2. if formatprg!="" run regular gq
|
|
|
|
"3. else run =
|
2013-03-15 18:12:09 -04:00
|
|
|
nnoremap <expr> gq <SID>set_formatprg() ? '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()
|
|
|
|
"Autoformat code
|
|
|
|
exe "normal gggqG"
|
|
|
|
"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()
|