summaryrefslogtreecommitdiffstats
path: root/vim/plugin/rainbow_paren.vim
blob: 3578d6357789bc0787dbfd2b72a7ddd15ff88905 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
" this script by Shawn M Moore aka Sartak <sartak at gmail.com>
" also by Michael R Geddes aka frogonwheels <vimmer at frog.wheelycreek.net>
" originally by anonymous

" this in the public domain
" last updated 25 Mar 07

" this does nothing unless you,
"   let g:rainbow = 1

" and set which kinds of character pairs you want to rainbow
"   let g:rainbow_paren   = 1 " ()
"   let g:rainbow_brace   = 1 " {}
"   let g:rainbow_bracket = 1 " []
"   let g:rainbow_angle   = 1 " <>

" if you want the different types to nest, such that the braces in ({}) are
" colored the same as the internal parens of (()), then
"   let g:rainbow_nested = 1

" you probably also want to use something like
"   autocmd BufReadPost * source /path/to/rainbow_parens.vim
"   autocmd BufNewFile  * source /path/to/rainbow_parens.vim

if exists("g:rainbow") && g:rainbow != 0
  hi level1c ctermfg=darkcyan
  hi level2c ctermfg=darkgreen
  hi level3c ctermfg=darkyellow
  hi level4c ctermfg=darkblue
  hi level5c ctermfg=darkmagenta
  hi level6c ctermfg=darkred      " this color is never nested, it only appears on the outermost layer

  " helper function
  func s:DoSyn(cur, top, left, right, uniq)
    let uniq = a:uniq
    if exists("g:rainbow_nested") && g:rainbow_nested != 0
      let uniq = ""
    endif

    let cmd = 'syn region level'.uniq.a:cur.' transparent fold matchgroup=level'.a:cur.'c start=/'.a:left.'/ end=/'.a:right.'/ contains=TOP'

    let i = a:cur

    if i == 1
      let i = a:top
    endif

    while i <= a:top
      let cmd = cmd . ',level' . uniq . i
      let i = i + 1
    endwhile
    exe cmd
  endfunc

  func s:DoSyntaxes(count)
    let i = 1

    while i <= a:count
      " if you define new pairs, make sure to take into account that the
      " delimiter is currently / and that it uses regex, so you need to escape
      " regex metachars (like what is done for brackets)

      if exists("g:rainbow_paren") && g:rainbow_paren != 0
        call s:DoSyn(i, a:count, "(", ")", "a")
      endif

      if exists("g:rainbow_brace") && g:rainbow_brace != 0
        call s:DoSyn(i, a:count, "{", "}", "b")
      endif

      if exists("g:rainbow_bracket") && g:rainbow_bracket != 0
        call s:DoSyn(i, a:count, "\\[", "\\]", "c")
      endif

      if exists("g:rainbow_angle") && g:rainbow_angle != 0
        call s:DoSyn(i, a:count, "<", ">", "d")
      endif

      let i = i + 1
    endwhile
  endfun

  call s:DoSyntaxes(6) " 6 is the number of colors we have

  delfun s:DoSyn
  delfun s:DoSyntaxes
endif