css.vim 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. " Changes: 1) Reacquire the line while checking comment lines.
  7. if exists("b:did_indent")
  8. finish
  9. endif
  10. let b:did_indent = 1
  11. setlocal indentexpr=GetCSSIndent()
  12. setlocal indentkeys=0{,0},!^F,o,O
  13. setlocal nosmartindent
  14. let b:undo_indent = "setl smartindent< indentkeys< indentexpr<"
  15. if exists("*GetCSSIndent")
  16. finish
  17. endif
  18. let s:keepcpo= &cpo
  19. set cpo&vim
  20. function s:prevnonblanknoncomment(lnum)
  21. let lnum = a:lnum
  22. while lnum > 1
  23. let lnum = prevnonblank(lnum)
  24. let line = getline(lnum)
  25. if line =~ '\*/'
  26. while lnum > 1 && line !~ '/\*'
  27. let lnum -= 1
  28. let line = getline(lnum)
  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
  40. endfunction
  41. function s:count_braces(lnum, count_open)
  42. let n_open = 0
  43. let n_close = 0
  44. let line = getline(a:lnum)
  45. let pattern = '[{}]'
  46. let i = match(line, pattern)
  47. while i != -1
  48. if synIDattr(synID(a:lnum, i + 1, 0), 'name') !~ 'css\%(Comment\|StringQ\{1,2}\)'
  49. if line[i] == '{'
  50. let n_open += 1
  51. elseif line[i] == '}'
  52. if n_open > 0
  53. let n_open -= 1
  54. else
  55. let n_close += 1
  56. endif
  57. endif
  58. endif
  59. let i = match(line, pattern, i + 1)
  60. endwhile
  61. return a:count_open ? n_open : n_close
  62. endfunction
  63. function GetCSSIndent()
  64. let line = getline(v:lnum)
  65. if line =~ '^\s*\*'
  66. return cindent(v:lnum)
  67. endif
  68. let pnum = s:prevnonblanknoncomment(v:lnum - 1)
  69. if pnum == 0
  70. return 0
  71. endif
  72. return indent(pnum) + s:count_braces(pnum, 1) * shiftwidth()
  73. \ - s:count_braces(v:lnum, 0) * shiftwidth()
  74. endfunction
  75. let &cpo = s:keepcpo
  76. unlet s:keepcpo