Skip to content

Commit

Permalink
Import
Browse files Browse the repository at this point in the history
  • Loading branch information
ciaranm committed Jul 9, 2009
0 parents commit 40a1e98
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 0 deletions.
40 changes: 40 additions & 0 deletions doc/detectindent.txt
@@ -0,0 +1,40 @@
*detectindent.txt* The Detect Indent Plugin 1.0, Jan 04, 2005

Author: Ciaran McCreesh <ciaran.mccreesh at googlemail.com>

==============================================================================
1. Contents *detectindent* *detectindent-contents*

1. Contents |detectindent-contents|
2. :DetectIndent Command |:DetectIndent|
Settings |detectindent-settings|
3. Uptime ChangeLog |uptime-changelog|

==============================================================================
2. :DetectIndent Command *:DetectIndent*

The :DetectIndent command tries to intelligently set the 'shiftwidth',
'expandtab' and 'tabstop' options based upon the existing settings in
use in the active file.

Settings *detectindent-settings*

When the correct value for 'expandtab' cannot be determined, it will
usually retain its existing value. To specify that 'expandtab' should
be used where autodetection is impossible, set: >
:let g:detectindent_preferred_expandtab = 1
< in your |vimrc| file.

To set a preferred value for 'shiftwidth' and 'tabstop' when they
cannot be automatically detected, set: >
:let g:detectindent_preferred_indent = 4
< in your |vimrc| file.

==============================================================================
3. DetectIndent ChangeLog *detectindent-changelog*

v1.0 (20050105)
* initial release after discussion on irc.freenode.net:#vim

==============================================================================
vim:tw=78:ts=8:ft=help
127 changes: 127 additions & 0 deletions plugin/detectindent.vim
@@ -0,0 +1,127 @@
" Name: detectindent (global plugin)
" Version: 1.0
" Author: Ciaran McCreesh <ciaran.mccreesh at googlemail.com>
" Updates: http://github.com/ciaranm/detectindent
" Purpose: Detect file indent settings
"
" License: You may redistribute this plugin under the same terms as Vim
" itself.
"
" Usage: :DetectIndent
"
" " to prefer expandtab to noexpandtab when detection is
" " impossible:
" :let g:detectindent_preferred_expandtab = 1
"
" " to set a preferred indent level when detection is
" " impossible:
" :let g:detectindent_preferred_indent = 4
"
" Requirements: Untested on Vim versions below 6.2

fun! <SID>IsCommentStart(line)
" &comments isn't reliable
if &ft == "c" || &ft == "cpp"
return -1 != match(a:line, '/\*')
else
return 0
endif
endfun

fun! <SID>IsCommentEnd(line)
if &ft == "c" || &ft == "cpp"
return -1 != match(a:line, '\*/')
else
return 0
endif
endfun

fun! <SID>DetectIndent()
let l:has_leading_tabs = 0
let l:has_leading_spaces = 0
let l:shortest_leading_spaces_run = 0
let l:longest_leading_spaces_run = 0

let l:idx_end = line("$")
let l:idx = 1
while l:idx <= l:idx_end
let l:line = getline(l:idx)

" try to skip over comment blocks, they can give really screwy indent
" settings in c/c++ files especially
if <SID>IsCommentStart(l:line)
while l:idx <= l:idx_end && ! <SID>IsCommentEnd(l:line)
let l:line = getline(l:idx)
let l:idx = l:idx + 1
endwhile
let l:idx = l:idx + 1
continue
endif

let l:leading_char = strpart(l:line, 0, 1)

if l:leading_char == "\t"
let l:has_leading_tabs = 1

elseif l:leading_char == " "
" only interested if we don't have a run of spaces followed by a
" tab.
if -1 == match(l:line, '^ \+\t')
let l:has_leading_spaces = 1
let l:spaces = strlen(matchstr(l:line, '^ \+'))
if l:shortest_leading_spaces_run == 0 ||
\ l:spaces < l:shortest_leading_spaces_run
let l:shortest_leading_spaces_run = l:spaces
endif
if l:spaces > l:longest_leading_spaces_run
let l:longest_leading_spaces_run = l:spaces
endif
endif

endif

let l:idx = l:idx + 1
endwhile

if l:has_leading_tabs && ! l:has_leading_spaces
" tabs only, no spaces
set noexpandtab
if exists("g:detectindent_preferred_tabsize")
let &shiftwidth = g:detectindent_preferred_indent
let &tabstop = g:detectindent_preferred_indent
endif

elseif l:has_leading_spaces && ! l:has_leading_tabs
" spaces only, no tabs
set expandtab
let &shiftwidth = l:shortest_leading_spaces_run

elseif l:has_leading_spaces && l:has_leading_tabs
" spaces and tabs
set noexpandtab
let &shiftwidth = l:shortest_leading_spaces_run

" mmmm, time to guess how big tabs are
if l:longest_leading_spaces_run < 2
let &tabstop = 2
elseif l:longest_leading_spaces_run < 4
let &tabstop = 4
else
let &tabstop = 8
endif

else
" no spaces, no tabs
if exists("g:detectindent_preferred_tabsize")
let &shiftwidth = g:detectindent_preferred_indent
let &tabstop = g:detectindent_preferred_indent
endif
if exists("g:detectindent_preferred_expandtab")
set expandtab
endif

endif
endfun

command! -nargs=0 DetectIndent call <SID>DetectIndent()

0 comments on commit 40a1e98

Please sign in to comment.