update detect indent

This commit is contained in:
David Majnemer 2010-10-22 02:30:09 -05:00
parent 5f7c671647
commit 702cd10313

View file

@ -1,7 +1,7 @@
" Name: detectindent (global plugin) " Name: detectindent (global plugin)
" Version: 1.0 " Version: 1.0
" Author: Ciaran McCreesh <ciaranm at gentoo.org> " Author: Ciaran McCreesh <ciaran.mccreesh at googlemail.com>
" Updates: http://dev.gentoo.org/~ciaranm/vim/ " Updates: http://github.com/ciaranm/detectindent
" Purpose: Detect file indent settings " Purpose: Detect file indent settings
" "
" License: You may redistribute this plugin under the same terms as Vim " License: You may redistribute this plugin under the same terms as Vim
@ -19,9 +19,14 @@
" "
" Requirements: Untested on Vim versions below 6.2 " Requirements: Untested on Vim versions below 6.2
if exists("loaded_detectindent")
finish
endif
let loaded_detectindent = 1
fun! <SID>IsCommentStart(line) fun! <SID>IsCommentStart(line)
" &comments isn't reliable " &comments aren't reliable
if &ft == "c" || &ft == "cpp" || &ft == "java" || &ft == "scala" if &ft == "c" || &ft == "cpp" || &ft == "javascript" || &ft == "java" || &ft == "scala"
return -1 != match(a:line, '/\*') return -1 != match(a:line, '/\*')
elseif &ft == "ocaml" " ocaml comments elseif &ft == "ocaml" " ocaml comments
return -1 != match(a:line, '(\*') return -1 != match(a:line, '(\*')
@ -33,10 +38,10 @@ fun! <SID>IsCommentStart(line)
endfun endfun
fun! <SID>IsCommentEnd(line) fun! <SID>IsCommentEnd(line)
if &ft == "c" || &ft == "cpp" || &ft == "java" || &ft == "scala" if &ft == "c" || &ft == "cpp" || &ft == "javascript" || &ft == "java" || &ft == "scala"
return -1 != match(a:line, '\*/') return -1 != match(a:line, '\*/')
elseif &ft == "ocaml" " ocaml comments elseif &ft == "ocaml" " ocaml comments
return -1 != match(a:line, '\*(') return -1 != match(a:line, '\*)')
elseif &ft == "perl" " catch POD elseif &ft == "perl" " catch POD
return -1 != match(a:line, '^=cut') return -1 != match(a:line, '^=cut')
else else
@ -49,6 +54,10 @@ fun! <SID>DetectIndent()
let l:has_leading_spaces = 0 let l:has_leading_spaces = 0
let l:shortest_leading_spaces_run = 0 let l:shortest_leading_spaces_run = 0
let l:longest_leading_spaces_run = 0 let l:longest_leading_spaces_run = 0
let l:max_lines = 1024
if exists("g:detectindent_max_lines_to_analyse")
let l:max_lines = g:detectindent_max_lines_to_analyse
endif
let l:idx_end = line("$") let l:idx_end = line("$")
let l:idx = 1 let l:idx = 1
@ -59,13 +68,20 @@ fun! <SID>DetectIndent()
" settings in c/c++ files especially " settings in c/c++ files especially
if <SID>IsCommentStart(l:line) if <SID>IsCommentStart(l:line)
while l:idx <= l:idx_end && ! <SID>IsCommentEnd(l:line) while l:idx <= l:idx_end && ! <SID>IsCommentEnd(l:line)
let l:line = getline(l:idx)
let l:idx = l:idx + 1 let l:idx = l:idx + 1
let l:line = getline(l:idx)
endwhile endwhile
let l:idx = l:idx + 1 let l:idx = l:idx + 1
continue continue
endif endif
" Skip lines that are solely whitespace, since they're less likely to
" be properly constructed.
if l:line !~ '\S'
let l:idx = l:idx + 1
continue
endif
let l:leading_char = strpart(l:line, 0, 1) let l:leading_char = strpart(l:line, 0, 1)
if l:leading_char == "\t" if l:leading_char == "\t"
@ -89,47 +105,58 @@ fun! <SID>DetectIndent()
endif endif
let l:idx = l:idx + 1 let l:idx = l:idx + 1
let l:max_lines = l:max_lines - 1
if l:max_lines == 0
let l:idx = l:idx_end + 1
endif
endwhile endwhile
if l:has_leading_tabs && ! l:has_leading_spaces if l:has_leading_tabs && ! l:has_leading_spaces
" tabs only, no spaces " tabs only, no spaces
set noexpandtab setl noexpandtab
if exists("g:detectindent_preferred_tabsize") if exists("g:detectindent_preferred_indent")
let &shiftwidth = g:detectindent_preferred_indent let &l:shiftwidth = g:detectindent_preferred_indent
let &tabstop = g:detectindent_preferred_indent let &l:tabstop = g:detectindent_preferred_indent
endif endif
elseif l:has_leading_spaces && ! l:has_leading_tabs elseif l:has_leading_spaces && ! l:has_leading_tabs
" spaces only, no tabs " spaces only, no tabs
set expandtab setl expandtab
let &shiftwidth = l:shortest_leading_spaces_run let &l:shiftwidth = l:shortest_leading_spaces_run
let &softtabstop = l:shortest_leading_spaces_run let &l:softtabstop = l:shortest_leading_spaces_run
elseif l:has_leading_spaces && l:has_leading_tabs elseif l:has_leading_spaces && l:has_leading_tabs
" spaces and tabs " spaces and tabs
set noexpandtab setl noexpandtab
let &shiftwidth = l:shortest_leading_spaces_run let &l:shiftwidth = l:shortest_leading_spaces_run
" mmmm, time to guess how big tabs are " mmmm, time to guess how big tabs are
if l:longest_leading_spaces_run < 2 if l:longest_leading_spaces_run < 2
let &tabstop = 2 let &l:tabstop = 2
elseif l:longest_leading_spaces_run < 4 elseif l:longest_leading_spaces_run < 4
let &tabstop = 4 let &l:tabstop = 4
else else
let &tabstop = 8 let &l:tabstop = 8
endif endif
else else
" no spaces, no tabs " no spaces, no tabs
if exists("g:detectindent_preferred_tabsize") if exists("g:detectindent_preferred_indent") &&
let &shiftwidth = g:detectindent_preferred_indent \ exists("g:detectindent_preferred_expandtab")
let &tabstop = g:detectindent_preferred_indent setl expandtab
endif let &l:shiftwidth = g:detectindent_preferred_indent
if exists("g:detectindent_preferred_expandtab") let &l:softtabstop = g:detectindent_preferred_indent
set expandtab elseif exists("g:detectindent_preferred_indent")
if exists("g:detectindent_preferred_tabsize") setl noexpandtab
let &softtabstop = l:shortest_leading_spaces_run let &l:shiftwidth = g:detectindent_preferred_indent
endif let &l:tabstop = g:detectindent_preferred_indent
elseif exists("g:detectindent_preferred_expandtab")
setl expandtab
else
setl noexpandtab
endif endif
endif endif