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