From f5e60b52661883676d6c398ab2a1cedcbc149977 Mon Sep 17 00:00:00 2001 From: Chiel92 Date: Tue, 26 Mar 2013 23:03:15 +0100 Subject: [PATCH] compatibilty with default gq functionality --- README.md | 3 +-- plugin/autoformat.vim | 18 +++++++++++------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 49b6ab4..9599bf0 100644 --- a/README.md +++ b/README.md @@ -98,7 +98,7 @@ As you see, this allows us to dynamically define some parameters. In this example, the indent width that astyle will use, depends on the buffer local value of `&shiftwidth`. So if you're editing a csharp file and change the `shiftwidth`, the `formatprg_args_expr_` will change correspondingly. -For the default formatprogram definitions, the options `expandtab` and `shiftwidth` are taken into account whenever possible. +For the default formatprogram definitions, the options `expandtab`, `shiftwidth` and `textwidth` are taken into account whenever possible. This means that the formatting style will match your current vim settings as much as possible. For the exact default definitions, have a look in `vim-autoformat/plugin/defaults.vim`. @@ -108,7 +108,6 @@ Todo list * Check for windows support. * Option for on-the-fly code-formatting, like visual studio (If ever. When you have a clever idea about how to do this, i'd be glad to hear.) * Create a help file. -* Take `textwidth` into account. * Only use autoindent as fallback when running `:Autoformat`. diff --git a/plugin/autoformat.vim b/plugin/autoformat.vim index ac38c76..4d009cb 100644 --- a/plugin/autoformat.vim +++ b/plugin/autoformat.vim @@ -42,18 +42,22 @@ function! s:set_formatprg() return 1 endfunction -"When gq has been pressed: -"1. set right formatprg -"2. if formatprg!="" run regular gq -"3. else run = -noremap gq set_formatprg() ? 'gq' : '=' +"set right formatprg before formatting +noremap gq set_formatprg() ? 'gq' : 'gq' "Function for formatting the entire buffer function! s:Autoformat() "Save window state let winview=winsaveview() - "Autoformat code - exe "normal gggqG" + + if set_formatprg() + "Autoformat code + exe "1,$!".&formatprg + else + "Autoindent code + exe "normal gg=G" + endif + "Recall window state call winrestview(winview) endfunction