diff --git a/README.md b/README.md index ba21b3e..7ed0c91 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,12 @@ let g:autoformat_remove_trailing_spaces = 0 To disable or re-enable these option for specific buffers, use the buffer local variants: `b:autoformat_autoindent`, `b:autoformat_retab` and `b:autoformat_remove_trailing_spaces`. +So to disable autoindent for filetypes that have incompetent indent files, use + +```vim +autocmd FileType vim,tex let b:autoformat_autoindent=0 +``` + You can manually autoindent, retab or remove trailing whitespace with the following respective commands. diff --git a/plugin/defaults.vim b/plugin/defaults.vim index 3a1349b..ff5bfcd 100644 --- a/plugin/defaults.vim +++ b/plugin/defaults.vim @@ -23,9 +23,20 @@ endif " Python if !exists('g:formatdef_autopep8') - let g:formatdef_autopep8 = '"autopep8 - --range ".a:firstline." ".a:lastline." ".(&textwidth ? "--max-line-length=".&textwidth : "")' + " Autopep8 will not do indentation fixes when a range is specified, so we + " only pass a range when there is a visual selection that is not the + " entire file. See #125. + let g:formatdef_autopep8 = '"autopep8 -".(g:DoesRangeEqualBuffer(a:firstline, a:lastline) ? " --range ".a:firstline." ".a:lastline : "")." ".(&textwidth ? "--max-line-length=".&textwidth : "")' endif +" There doesn't seem to be a reliable way to detect if are in some kind of visual mode, +" so we use this as a workaround. We compare the length of the file against +" the range arguments. If there is no range given, the range arguments default +" to the entire file, so we return false if the range comprises the entire file. +function! g:DoesRangeEqualBuffer(first, last) + return line('$') != a:last - a:first + 1 +endfunction + if !exists('g:formatters_python') let g:formatters_python = ['autopep8'] endif