aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2020-05-30 01:06:58 +1200
committerTom Ryder <tom@sanctum.geek.nz>2020-05-30 01:06:58 +1200
commit3ea769e79a308dc6ce73c110add748919fe6781d (patch)
tree1c890cb672bfab248fe572d09759a59320061514
parentPrototype ready for indent switcher (diff)
downloaddotfiles-3ea769e79a308dc6ce73c110add748919fe6781d.tar.gz
dotfiles-3ea769e79a308dc6ce73c110add748919fe6781d.zip
Split detect_indent.vim into autoload/plugin
Add load guard etc
-rw-r--r--vim/autoload/detect_indent.vim57
-rw-r--r--vim/plugin/detect_indent.vim72
2 files changed, 70 insertions, 59 deletions
diff --git a/vim/autoload/detect_indent.vim b/vim/autoload/detect_indent.vim
new file mode 100644
index 00000000..91952032
--- /dev/null
+++ b/vim/autoload/detect_indent.vim
@@ -0,0 +1,57 @@
+function s:CompareNumeric(a, b)
+ return a:a > a:b ? 1 : -1
+endfunction
+
+function detect_indent#() 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
diff --git a/vim/plugin/detect_indent.vim b/vim/plugin/detect_indent.vim
index 8bffde42..fef4b65e 100644
--- a/vim/plugin/detect_indent.vim
+++ b/vim/plugin/detect_indent.vim
@@ -1,59 +1,13 @@
-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()
+if exists('loaded_detect_indent') || &compatible
+ finish
+endif
+let loaded_detect_indent = 1
+
+command! -bar DetectIndent
+ \ call detect_indent#()
+
+augroup detect_indent
+ autocmd!
+ autocmd FileType *
+ \ DetectIndent
+augroup END