Patch 7.0.084
[MacVim.git] / runtime / indent / css.vim
blobbe1c4d73ecc96a7d86cde74875404e23f834660d
1 " Vim indent file
2 " Language:         CSS
3 " Maintainer:       Nikolai Weibull <now@bitwi.se>
4 " Latest Revision:  2006-04-19
6 if exists("b:did_indent")
7   finish
8 endif
9 let b:did_indent = 1
11 setlocal indentexpr=GetCSSIndent()
12 setlocal indentkeys=0{,0},!^F,o,O
14 if exists("*GetCSSIndent")
15   finish
16 endif
18 function 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
37 endfunction
39 function 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
60 endfunction
62 function 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
70   let pnum = s:prevnonblanknoncomment(v:lnum - 1)
71   if pnum == 0
72     return 0
73   endif
75   let ind = indent(pnum) + s:count_braces(pnum, 1) * &sw
77   let pline = getline(pnum)
78   if pline =~ '}\s*$'
79     let ind -= (s:count_braces(pnum, 0) - (pline =~ '^\s*}' ? 1 : 0)) * &sw
80   endif
82   return ind
83 endfunction