aboutsummaryrefslogtreecommitdiff
path: root/vim
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2020-05-30 00:58:05 +1200
committerTom Ryder <tom@sanctum.geek.nz>2020-05-30 00:58:05 +1200
commit5950f4bc835ef0b5e96b18d2adfca8574c97fb2c (patch)
tree9946ed0ee0f5a6164e8bf6e87733580bf1ad6903 /vim
parentCommit a much simpler approach to indent guessing (diff)
downloaddotfiles-5950f4bc835ef0b5e96b18d2adfca8574c97fb2c.tar.gz
dotfiles-5950f4bc835ef0b5e96b18d2adfca8574c97fb2c.zip
Prototype ready for indent switcher
Diffstat (limited to 'vim')
-rw-r--r--vim/plugin/detect_indent.vim30
1 files changed, 23 insertions, 7 deletions
diff --git a/vim/plugin/detect_indent.vim b/vim/plugin/detect_indent.vim
index f8dd8966..8bffde42 100644
--- a/vim/plugin/detect_indent.vim
+++ b/vim/plugin/detect_indent.vim
@@ -1,3 +1,7 @@
+function s:CompareNumeric(a, b)
+ return a:a > a:b ? 1 : -1
+endfunction
+
function Count() abort
let spaces = {
@@ -5,7 +9,7 @@ function Count() abort
\ 'total': 0,
\}
let tabs = 0
- let total = line('$')
+ let total = max([line('$'), 1024])
for lnum in range(1, total)
let line = getline(lnum)
@@ -21,21 +25,33 @@ function Count() abort
let spaces['total'] += 1
endfor
- if &expandtab && tabs > spaces['total'] * 5
+ if &expandtab == (spaces['total'] > tabs)
+ return
+ endif
+
+ if &expandtab
setlocal noexpandtab softtabstop=0
let &l:shiftwidth = &tabstop
- setlocal expandtab?
- elseif !&expandtab && spaces['total'] > tabs * 5
+ else
+ setlocal expandtab
let shiftwidth = 0
- for shiftwidth in sort(keys(spaces['hist']))
+ let indents = keys(spaces['hist'])
+ call map(indents, 'str2nr(v:val)')
+ call sort(indents, 's:CompareNumeric')
+ for shiftwidth in indents
if spaces['hist'][shiftwidth] * 100 / spaces['total'] >= 5
break
endif
endfor
- setlocal expandtab
let &l:shiftwidth = shiftwidth
let &l:softtabstop = shiftwidth
- setlocal expandtab? shiftwidth?
+ endif
+
+ let undo_indent = 'setlocal expandtab? shiftwidth? softtabstop?'
+ if exists('b:undo_indent')
+ let b:undo_indent .= '|'.undo_indent
+ else
+ let b:undo_indent = undo_indent
endif
endfunction