1" Vim indent file 2" Language: CSS 3" Maintainer: Nikolai Weibull <[email protected]> 4" Latest Revision: 2010-12-22 5 6if exists("b:did_indent") 7 finish 8endif 9let b:did_indent = 1 10 11setlocal indentexpr=GetCSSIndent() 12setlocal indentkeys=0{,0},!^F,o,O 13setlocal nosmartindent 14 15if exists("*GetCSSIndent") 16 finish 17endif 18let s:keepcpo= &cpo 19set cpo&vim 20 21function s:prevnonblanknoncomment(lnum) 22 let lnum = a:lnum 23 while lnum > 1 24 let lnum = prevnonblank(lnum) 25 let line = getline(lnum) 26 if line =~ '\*/' 27 while lnum > 1 && line !~ '/\*' 28 let lnum -= 1 29 endwhile 30 if line =~ '^\s*/\*' 31 let lnum -= 1 32 else 33 break 34 endif 35 else 36 break 37 endif 38 endwhile 39 return lnum 40endfunction 41 42function s:count_braces(lnum, count_open) 43 let n_open = 0 44 let n_close = 0 45 let line = getline(a:lnum) 46 let pattern = '[{}]' 47 let i = match(line, pattern) 48 while i != -1 49 if synIDattr(synID(a:lnum, i + 1, 0), 'name') !~ 'css\%(Comment\|StringQ\{1,2}\)' 50 if line[i] == '{' 51 let n_open += 1 52 elseif line[i] == '}' 53 if n_open > 0 54 let n_open -= 1 55 else 56 let n_close += 1 57 endif 58 endif 59 endif 60 let i = match(line, pattern, i + 1) 61 endwhile 62 return a:count_open ? n_open : n_close 63endfunction 64 65function GetCSSIndent() 66 let line = getline(v:lnum) 67 if line =~ '^\s*\*' 68 return cindent(v:lnum) 69 endif 70 71 let pnum = s:prevnonblanknoncomment(v:lnum - 1) 72 if pnum == 0 73 return 0 74 endif 75 76 return indent(pnum) + s:count_braces(pnum, 1) * &sw 77 \ - s:count_braces(v:lnum, 0) * &sw 78endfunction 79 80let &cpo = s:keepcpo 81unlet s:keepcpo 82