blob: 8bffde4217e49cc24d329f083381b5d2af5a76fb (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
function s:CompareNumeric(a, b)
return a:a > a:b ? 1 : -1
endfunction
function Count() abort
let spaces = {
\ 'hist': {},
\ 'total': 0,
\}
let tabs = 0
let total = max([line('$'), 1024])
for lnum in range(1, total)
let line = getline(lnum)
let tabs += matchstr(line, '^\t*') != ''
let indent = strlen(matchstr(line, '^ *'))
if indent == 0
continue
endif
if !has_key(spaces['hist'], indent)
let spaces['hist'][indent] = 0
endif
let spaces['hist'][indent] += 1
let spaces['total'] += 1
endfor
if &expandtab == (spaces['total'] > tabs)
return
endif
if &expandtab
setlocal noexpandtab softtabstop=0
let &l:shiftwidth = &tabstop
else
setlocal expandtab
let shiftwidth = 0
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
let &l:shiftwidth = shiftwidth
let &l:softtabstop = 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
autocmd FileType * call Count()
|