Vim脚本: If block的match pair
时间:2008-07-16 来源:ThomasLiu83
因为需要在代码中配对if和end if,所有需要写个vim的脚本,来做这些,使得自己不会被很多的嵌套If所淹没...
1 function Set_IfEnd_Syn()
2 syn region If_End_Src start="@@@" end="@@@"
3 hi If_End_Src guibg=SeaGreen
4 endfun
5
6 function Del_mark()
7 let s:cur_line=line(".")
8 if g:IfEnd_Line1>0 || g:IfEnd_Line2>0
9 exe "normal".g:IfEnd_Line1."G"
10 let s:tmpstr=getline(".")
11 if match(s:tmpstr,"@@@")>=0
12 exe "normal \<home>3x"
13 endif
14 exe "normal".g:IfEnd_Line2."G"
15 let s:tmpstr=getline(".")
16 if match(s:tmpstr,"@@@")>=0
17 exe "normal \<end>XXx"
18 endif
19 let g:IfEnd_Line1=0
20 let g:IfEnd_Line2=0
21 endif
22 exec "normal ".s:cur_line."G"
23 endfun
24
25 function Add_mark()
26 if g:IfEnd_Line1>0 && g:IfEnd_Line2>g:IfEnd_Line1
27 exec "normal ".g:IfEnd_Line2."G"
28 exec "normal \<end>a@@@\<esc>"
29 exec "normal ".g:IfEnd_Line1."G"
30 exe "normal \<home>i@@@\<esc>"
31 endif
32 endfun
33
34 function Search_End()
35 call Del_mark()
36 exec "normal 0"
37 exec "normal /^\\s*if\\>\<CR>"
38 let g:IfEnd_Line1=line(".")
39 let s:end_count=1
40 while s:end_count>0
41 exec "normal j"
42 let s:str=getline(".")
43 let s:result = match(s:str,"^\\s*if\\>")
44 if s:result >= 0
45 let s:end_count = s:end_count + 1
46 else
47 let s:result=match(s:str,"^\\s*end if\\>")
48 if s:result >=0
49 let s:end_count = s:end_count - 1
50 endif
51 endif
52 endwhile
53
54 let g:IfEnd_Line2=line(".")
55 call Add_mark()
56 endfun
57
58 function Search_If()
59 call Del_mark()
60 exec "normal 0"
61 exec "normal /\\<end if\\>\<CR>"
62 let g:IfEnd_Line2=line(".")
63 let s:end_count=1
64 while s:end_count>0
65 exec "normal k"
66 let s:str=getline(".")
67 let s:result = match(s:str,"^\\s*end if\\>")
68 if s:result >= 0
69 let s:end_count = s:end_count + 1
70 else
71 let s:result=match(s:str,"^\\s*if\\>")
72 if s:result >=0
73 let s:end_count = s:end_count - 1
74 endif
75 endif
76 endwhile
77
78 let g:IfEnd_Line2=line(".")
79 call Add_mark()
80 endfun
81
82 map <F2> :call Search_End()<CR>
83 map <F3> :call Search_If()<CR>
84 au BufRead * :call Set_IfEnd_Syn()
85