css.vim 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. " Vim indent file
  2. " Language: CSS
  3. " Maintainer: Nikolai Weibull <now@bitwi.se>
  4. " Latest Revision: 2012-05-30
  5. " Use of shiftwidth() added by Oleg Zubchenko.
  6. if exists("b:did_indent")
  7. finish
  8. endif
  9. let b:did_indent = 1
  10. setlocal indentexpr=GetCSSIndent()
  11. setlocal indentkeys=0{,0},!^F,o,O
  12. setlocal nosmartindent
  13. let b:undo_indent = "setl smartindent< indentkeys< indentexpr<"
  14. if exists("*GetCSSIndent")
  15. finish
  16. endif
  17. let s:keepcpo= &cpo
  18. set cpo&vim
  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. 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
  61. function GetCSSIndent()
  62. let line = getline(v:lnum)
  63. if line =~ '^\s*\*'
  64. return cindent(v:lnum)
  65. endif
  66. let pnum = s:prevnonblanknoncomment(v:lnum - 1)
  67. if pnum == 0
  68. return 0
  69. endif
  70. return indent(pnum) + s:count_braces(pnum, 1) * shiftwidth()
  71. \ - s:count_braces(v:lnum, 0) * shiftwidth()
  72. endfunction
  73. let &cpo = s:keepcpo
  74. unlet s:keepcpo