- commit
- 9e851a2d5c7acbd2c6a5f955cfac553be2a50137
- parent
- 69857b8f1b0196dda175b086d31ca37b44cfa301
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2019-02-23 18:12
use autoload and explicit l: variables
Diffstat
| A | autoload/detectindent.vim | 37 | +++++++++++++++++++++++++++++++++++++ |
| M | plugin/detectindent.vim | 44 | +++----------------------------------------- |
2 files changed, 40 insertions, 41 deletions
diff --git a/autoload/detectindent.vim b/autoload/detectindent.vim
@@ -0,0 +1,37 @@ -1 1 function! detectindent#Set(...) -1 2 if a:0 < 1 -1 3 let &l:expandtab = g:detectindent_preferred_expandtab -1 4 else -1 5 let &l:expandtab = a:1 -1 6 endif -1 7 -1 8 if a:0 < 2 -1 9 let l:width = g:detectindent_preferred_indent -1 10 else -1 11 let l:width = a:2 -1 12 endif -1 13 -1 14 let &l:tabstop = l:width -1 15 let &l:shiftwidth = l:width -1 16 let &l:softtabstop = l:width -1 17 endfunction -1 18 -1 19 function! detectindent#Detect() -1 20 let l:n = 1 -1 21 -1 22 while l:n <= 50 -1 23 let l:line = getline(n) -1 24 -1 25 if l:line =~ '^ ' -1 26 call detectindent#Set(0) -1 27 return -1 28 elseif l:line =~ '^ ' -1 29 call detectindent#Set(1, strlen(matchstr(l:line, '^ \+'))) -1 30 return -1 31 endif -1 32 -1 33 let l:n = l:n + 1 -1 34 endwhile -1 35 -1 36 call detectindent#Set() -1 37 endfunction
diff --git a/plugin/detectindent.vim b/plugin/detectindent.vim
@@ -5,45 +5,7 @@ if !exists('g:detectindent_preferred_expandtab')
5 5 let g:detectindent_preferred_expandtab = 1
6 6 endif
7 7
8 -1 function! SetIndent(...)
9 -1 if a:0 < 1
10 -1 let &l:expandtab = g:detectindent_preferred_expandtab
11 -1 else
12 -1 let &l:expandtab = a:1
13 -1 endif
-1 8 command! -nargs=? Tabs call detectindent#Set(0, <f-args>)
-1 9 command! -nargs=? Spaces call detectindent#Set(1, <f-args>)
14 10
15 -1 if a:0 < 2
16 -1 let width = g:detectindent_preferred_indent
17 -1 else
18 -1 let width = a:2
19 -1 endif
20 -1
21 -1 let &l:tabstop = width
22 -1 let &l:shiftwidth = width
23 -1 let &l:softtabstop = width
24 -1 endfunction
25 -1
26 -1 function! DetectIndent()
27 -1 let n = 1
28 -1
29 -1 while n <= 50
30 -1 let line = getline(n)
31 -1
32 -1 if line =~ '^ '
33 -1 call SetIndent(0)
34 -1 return
35 -1 elseif line =~ '^ '
36 -1 call SetIndent(1, strlen(matchstr(line, '^ \+')))
37 -1 return
38 -1 endif
39 -1
40 -1 let n = n + 1
41 -1 endwhile
42 -1
43 -1 call SetIndent()
44 -1 endfunction
45 -1
46 -1 command! -nargs=? Tabs call SetIndent(0, <f-args>)
47 -1 command! -nargs=? Spaces call SetIndent(1, <f-args>)
48 -1
49 -1 autocmd BufNewFile,BufRead * call DetectIndent()
-1 11 autocmd BufNewFile,BufRead * call detectindent#Detect()