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