From e97d0fda6017b69effe8a56016bd5765293c951f Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sat, 14 Nov 2020 23:14:36 +1300 Subject: Avoid changing indent settings unnecessarily --- autoload/detect_indent.vim | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/autoload/detect_indent.vim b/autoload/detect_indent.vim index ab3f485..bae5f7c 100644 --- a/autoload/detect_indent.vim +++ b/autoload/detect_indent.vim @@ -61,25 +61,20 @@ function! detect_indent#() abort endfor - " If the value for 'expandtab' as determined by the user's configuration - " matches the expected dominance of space-indented lines over tab-indented - " lines, we're already using the right setting, and we stop here - " - if &expandtab == (spaces['total'] >= tabs) - return - endif - - " If 'expandtab' is set, we need to unset it and switch to pure tab + " If 'expandtab' is set, but more lines in the existing buffer have tab + " indents than space indents, we need to unset it and switch to pure tab " indenting; that's straightforward, we just need to make sure 'shiftwidth' " matches 'tabstop', and that 'softtabstop' is off. " - if &expandtab + if &expandtab && tabs > spaces['total'] setlocal noexpandtab softtabstop=0 let &l:shiftwidth = &tabstop - " If 'expandtab' is not set, we need to set it, and guess an appropriate + " If 'expandtab' is unset, but more lines in the existing buffer have space + " indents than tab indents, we need to set it, and guess an appropriate " 'shiftwidth'. - else + " + elseif !&expandtab && tabs < spaces['total'] setlocal expandtab " Iterate through the keys of the histogram from smallest to largest. @@ -102,6 +97,10 @@ function! detect_indent#() abort let &l:shiftwidth = shiftwidth let &l:softtabstop = shiftwidth + " If the 'expandtab' setting looks like it was already accurate to the + " buffer, stop here + else + return endif " Since we just messed with indent settings, stack up a command to revert -- cgit v1.2.3