diff --git a/README.md b/README.md index 0d1cadc..9218ba7 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,8 @@ Remember that when no formatprogram exists for a certain filetype, vim-autoforma This will fix at least the indentation of your code, according to vim's indentfile for that filetype. When you have installed the formatters you need, you can format the entire buffer with the command `:Autoformat`. +You can provide the command with a file type such as `:Autoformat json`, otherwise the buffer's filetype will be used. + For convenience it is recommended that you assign a key for this, like so: ```vim diff --git a/plugin/autoformat.vim b/plugin/autoformat.vim index 4e144c3..f5d182e 100644 --- a/plugin/autoformat.vim +++ b/plugin/autoformat.vim @@ -1,14 +1,16 @@ "Function for finding and setting the formatter with the given name -function! s:set_formatprg() - "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 +function! s:set_formatprg(...) + let type = a:0 ? a:1 : &filetype + + "Get formatprg config + 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 if !exists(s:formatprg_var) "No formatprg defined if exists("g:autoformat_verbosemode") - echoerr "No formatter defined for filetype '".&filetype."'." + echoerr "No formatter defined for filetype '".type."'." endif return 0 endif @@ -38,11 +40,11 @@ endfunction noremap gq set_formatprg() ? 'gq' : 'gq' "Function for formatting the entire buffer -function! s:Autoformat() +function! s:Autoformat(...) "Save window state let winview=winsaveview() - if set_formatprg() + if call('set_formatprg', a:000) "Autoformat code exe "1,$!".&formatprg else @@ -55,4 +57,4 @@ function! s:Autoformat() endfunction "Create a command for formatting the entire buffer -command! Autoformat call s:Autoformat() +command! -nargs=? -complete=filetype Autoformat call s:Autoformat()