用visualmark快速定位inst
今天发现了一个插件:visualmark,相当于一个书签,按ctrl+F2可以将某一行作为书签,高亮显示。F2可以在标注的书签之间切换。mm也有F2同样的功能。
下载地址:http://www.vim.org/scripts/script.php?script_id=1026
安装方法:直接放到插件文件夹$VIM/plugin
虽然这个书签在平时没什么大用,但是可以用他来实现一下例化模块的快速查找。
在低层模块比较多的模块中,尤其是顶层文件,taglist只能显示component,不能指定到具体例化的位置。用该插件自动标注“_inst”的位置,F2来切换跳转,虽然不能有单独的列表,但也比以前没有的时候方便了点。(自己不会写插件,改改插件还是可以的)。
实现后大概就是下面的样子,打开文件自动标注inst:
下面是实现过程:
1 仍然要修改插件的代码,在visualmark.vim中加入以下函数:
"------------------------------------------------------------------------------ "Add by myself "Function : AutoMarkInst() "Description : Auto Mark The line of inst " Must set the inst name such as : abc_inst : abc " Must have the plugin "visualmark" "------------------------------------------------------------------------------ " AutoMarkInst: function AutoMarkInst() let line = 1 while line <= line("$") let flag_mark = match(getline(line),"_inst") if (flag_mark != -1) let curr_line_number = line let sign_id = s:Vm_get_sign_id_from_line(curr_line_number) if sign_id < 0 let is_on = 0 else let is_on = 1 endif if (is_on != 1) if !exists("b:Vm_sign_number") let b:Vm_sign_number = 1 endif exe 'sign define SignSymbol linehl=SignColor texthl=SignColor' exe 'sign place ' . b:Vm_sign_number . ' line=' . line . ' name=SignSymbol buffer=' . winbufnr(0) let vsn = b:Vm_sign_number let b:Vm_sign_number = b:Vm_sign_number + 1 endif endif let line = line + 1 endwhile endfunction autocmd BufWinEnter *.vhd call AutoMarkInst()
2 颜色可以自己修改,在插件开头位置,我把原来的颜色换掉了:
if &bg == "dark" " highlight SignColor ctermfg=white ctermbg=blue guifg=white guibg=RoyalBlue3 highlight SignColor ctermfg=black ctermbg=darkmagenta guifg=white guibg=darkcyan else highlight SignColor ctermbg=white ctermfg=blue guibg=grey guifg=RoyalBlue3 endif
3 快捷键太多了,把mm这个快捷键去掉了
" map <silent> <unique> mm <Plug>Vm_toggle_sign
4 vimrc中原来设置F2是保存,我一般都使用“,w”,不用F2按键,现在改成切换mark也可以。
PS: 例化时写成这种模式:abc_inst : abc ,才能检测到。
把整个插件的源文档贴在这:
visualmark.vim
1 " Visual Mark
2 " 2005-10-27, brian wang
3 "
4 " Acknowledgements:
5 " - Thanks to Hari Krishna Dara's genutils.vim (http://vim.sourceforge.net/scripts/script.php?script_id=197)
6 " - Thanks to Mr. Charles E. Campbell, Jr. for making this script more plugin-like :-)
7 " - Thanks to Mr. Charles E. Campbell, Jr. for making this script adapt to
8 " dark/light backgrounds
9 " - Thanks to Evgeny Filatov for noticing a nasty bug in Vm_get_line_number :-)
10
11 if exists("loaded_VisualMark")
12 finish
13 endif
14 let loaded_VisualMark = 1
15 if !has("signs")
16 echoerr "***sorry*** [".expand("%")."] your vim doesn't support signs"
17 finish
18 endif
19
20 if &bg == "dark"
21 " highlight SignColor ctermfg=white ctermbg=blue guifg=white guibg=RoyalBlue3
22 highlight SignColor ctermfg=black ctermbg=darkmagenta guifg=white guibg=darkcyan
23 else
24 highlight SignColor ctermbg=white ctermfg=blue guibg=grey guifg=RoyalBlue3
25 endif
26
27 " ---------------------------------------------------------------------
28 " Public Interface:
29 if !hasmapto('<Plug>Vm_toggle_sign')
30 map <unique> <c-F2> <Plug>Vm_toggle_sign
31 " map <silent> <unique> mm <Plug>Vm_toggle_sign
32 endif
33 nnoremap <silent> <script> <Plug>Vm_toggle_sign :call Vm_toggle_sign()<cr>
34
35 if !hasmapto('<Plug>Vm_goto_next_sign')
36 map <unique> <F2> <Plug>Vm_goto_next_sign
37 endif
38 nnoremap <silent> <script> <Plug>Vm_goto_next_sign :call Vm_goto_next_sign()<cr>
39
40 if !hasmapto('<Plug>Vm_goto_prev_sign')
41 map <unique> <s-F2> <Plug>Vm_goto_prev_sign
42 endif
43 nnoremap <silent> <script> <Plug>Vm_goto_prev_sign :call Vm_goto_prev_sign()<cr>
44
45 " ---------------------------------------------------------------------
46 " GetVimCmdOutput:
47 " Stole from Hari Krishna Dara's genutils.vim (http://vim.sourceforge.net/scripts/script.php?script_id=197)
48 " to ease the scripts dependency issue
49 fun! s:GetVimCmdOutput(cmd)
50 " call Dfunc("GetVimCmdOutput(cmd.".a:cmd.">)")
51
52 " Save the original locale setting for the messages
53 let old_lang = v:lang
54
55 " Set the language to English
56 exec ":lan mes en_US"
57
58 let v:errmsg = ''
59 let output = ''
60 let _z = @z
61
62 try
63 redir @z
64 silent exe a:cmd
65 catch /.*/
66 let v:errmsg = substitute(v:exception, '^[^:]\+:', '', '')
67 finally
68 redir END
69 if v:errmsg == ''
70 let output = @z
71 endif
72 let @z = _z
73 endtry
74
75 " Restore the original locale
76 exec ":lan mes " . old_lang
77
78 " call Dret("GetVimCmdOutput <".output.">")
79 return output
80 endfun
81
82 " ---------------------------------------------------------------------
83 " Vm_place_sign:
84 fun! s:Vm_place_sign()
85 " call Dfunc("Vm_place_sign()")
86
87 if !exists("b:Vm_sign_number")
88 let b:Vm_sign_number = 1
89 endif
90
91 let ln = line(".")
92
93 exe 'sign define SignSymbol linehl=SignColor texthl=SignColor'
94 exe 'sign place ' . b:Vm_sign_number . ' line=' . ln . ' name=SignSymbol buffer=' . winbufnr(0)
95
96 let vsn = b:Vm_sign_number
97 let b:Vm_sign_number = b:Vm_sign_number + 1
98
99 " call Dret("Vm_place_sign : sign#".vsn." line#".ln." buf#".winbufnr(0))
100 endfun
101
102 " ---------------------------------------------------------------------
103 " Vm_remove_sign:
104 fun! s:Vm_remove_sign(sign_id)
105 " call Dfunc("Vm_remove_sign(sign_id=".a:sign_id.")")
106 silent! exe 'sign unplace ' . a:sign_id . ' buffer=' . winbufnr(0)
107 " call Dret("Vm_remove_sign")
108 endfun
109
110 " ---------------------------------------------------------------------
111 " Vm_remove_all_signs:
112 fun! s:Vm_remove_all_signs()
113 " call Dfunc("Vm_remove_all_signs()")
114 silent! exe 'sign unplace *'
115 " call Dret("Vm_remove_all_signs")
116 endfun
117
118 " ---------------------------------------------------------------------
119 " Vm_get_sign_id_from_line:
120 fun! s:Vm_get_sign_id_from_line(line_number)
121 " call Dfunc("Vm_get_sign_id_from_line(line_number=".a:line_number.")")
122
123 let sign_list = s:GetVimCmdOutput('sign place buffer=' . winbufnr(0))
124 " call Decho(sign_list)
125
126 let line_str_index = match(sign_list, "line=" . a:line_number, 0)
127 if line_str_index < 0
128 " call Dret("Vm_get_sign_id_from_line -1")
129 return -1
130 endif
131
132 let id_str_index = matchend(sign_list, "id=", line_str_index)
133 " let tmp = strpart(sign_list, id_str_index, 10) "Decho
134 " call Decho("ID str index: " . tmp)
135 if id_str_index < 0
136 " call Dret("Vm_get_sign_id_from_line -1")
137 return -1
138 endif
139
140 let space_index = match(sign_list, " ", id_str_index)
141 let id = strpart(sign_list, id_str_index, space_index - id_str_index)
142
143 " call Dret("Vm_get_sign_id_from_line ".id)
144 return id
145 endfun
146
147 " ---------------------------------------------------------------------
148 " Vm_toggle_sign:
149 fun! Vm_toggle_sign()
150 " call Dfunc("Vm_toggle_sign()")
151
152 let curr_line_number = line(".")
153 let sign_id = s:Vm_get_sign_id_from_line(curr_line_number)
154
155 if sign_id < 0
156 let is_on = 0
157 else
158 let is_on = 1
159 endif
160
161 if (is_on != 0)
162 call s:Vm_remove_sign(sign_id)
163 else
164 call s:Vm_place_sign()
165 endif
166
167 " call Dret("Vm_toggle_sign")
168 endfun
169
170 " ---------------------------------------------------------------------
171 " Vm_get_line_number:
172 fun! s:Vm_get_line_number(string)
173 " call Dfunc("Vm_get_line_number(string<".a:string.">)")
174
175 let line_str_index = match(a:string, "line=", b:Vm_start_from)
176 if line_str_index <= 0
177 " call Dret("Vm_get_line_number -1")
178 return -1
179 endif
180
181 let equal_sign_index = match(a:string, "=", line_str_index)
182 let space_index = match(a:string, " ", equal_sign_index)
183 let line_number = strpart(a:string, equal_sign_index + 1, space_index - equal_sign_index - 1)
184 let b:Vm_start_from = space_index
185
186 " call Dret("Vm_get_line_number ".line_number." : =indx:".equal_sign_index." _indx=".space_index)
187 return line_number + 0
188 endfun
189
190 " ---------------------------------------------------------------------
191 " Vm_get_next_sign_line:
192 fun! s:Vm_get_next_sign_line(curr_line_number)
193 " call Dfunc("Vm_get_next_sign_line(curr_line_number=".a:curr_line_number.">)")
194
195 let b:Vm_start_from = 1
196 let sign_list = s:GetVimCmdOutput('sign place buffer=' . winbufnr(0))
197 " call Decho("sign_list<".sign_list.">")
198
199 let curr_line_number = a:curr_line_number
200 let line_number = 1
201 let is_no_sign = 1
202 let min_line_number = -1
203 let min_line_number_diff = 0
204
205 while 1
206 let line_number = s:Vm_get_line_number(sign_list)
207 if line_number < 0
208 break
209 endif
210
211 " Record the very first line that has a sign
212 if is_no_sign != 0
213 let min_line_number = line_number
214 elseif line_number < min_line_number
215 let min_line_number = line_number
216 endif
217 let is_no_sign = 0
218
219 " let tmp_diff = curr_line_number - line_number
220 let tmp_diff = line_number - curr_line_number
221 if tmp_diff > 0
222 " line_number is below curr_line_number
223 if min_line_number_diff > 0
224 if tmp_diff < min_line_number_diff
225 let min_line_number_diff = tmp_diff
226 endif
227 else
228 let min_line_number_diff = tmp_diff
229 endif
230 endif
231
232 " call Decho("[DBG] Line Diff: #" . min_line_number_diff)
233 endwhile
234
235 let line_number = curr_line_number + min_line_number_diff
236 " call Decho("[DBG] Line Diff: #" . min_line_number_diff)
237 " call Decho("[DBG] Line Num: #" . line_number)
238
239 if is_no_sign != 0 || min_line_number_diff <= 0
240 let line_number = min_line_number
241 endif
242
243 " call Dret("Vm_get_next_sign_line ".line_number . " XXX")
244 return line_number
245 endfun
246
247 " ---------------------------------------------------------------------
248 " Vm_get_prev_sign_line:
249 fun! s:Vm_get_prev_sign_line(curr_line_number)
250 " call Dfunc("Vm_get_prev_sign_line(curr_line_number=".a:curr_line_number.">)")
251
252 let b:Vm_start_from = 1
253 let sign_list = s:GetVimCmdOutput('sign place buffer=' . winbufnr(0))
254 " call Decho("sign_list<".sign_list.">")
255
256 let curr_line_number = a:curr_line_number
257 let line_number = 1
258 let is_no_sign = 1
259 let max_line_number = -1
260 let max_line_number_diff = 0
261
262 while 1
263 let line_number = s:Vm_get_line_number(sign_list)
264 if line_number < 0
265 break
266 endif
267
268 " Record the very first line that has a sign
269 if is_no_sign != 0
270 let max_line_number = line_number
271 elseif line_number > max_line_number
272 let max_line_number = line_number
273 endif
274 let is_no_sign = 0
275
276 let tmp_diff = curr_line_number - line_number
277 if tmp_diff > 0
278 " line_number is below curr_line_number
279 if max_line_number_diff > 0
280 if tmp_diff < max_line_number_diff
281 let max_line_number_diff = tmp_diff
282 endif
283 else
284 let max_line_number_diff = tmp_diff
285 endif
286 endif
287
288 " call Decho("[DBG] Line Diff: #" . max_line_number_diff)
289 " call Decho("[DBG] Tmp Diff: #" . tmp_diff)
290 endwhile
291
292 let line_number = curr_line_number - max_line_number_diff
293 " call Decho("[DBG] Line Diff: #" . max_line_number_diff)
294 " call Decho("[DBG] Line Num: #" . line_number)
295
296 if is_no_sign != 0 || max_line_number_diff <= 0
297 let line_number = max_line_number
298 endif
299
300 " call Dret("Vm_get_prev_sign_line ".line_number . " XXX")
301 return line_number
302 endfun
303
304 " ---------------------------------------------------------------------
305 " Vm_goto_next_sign:
306 fun! Vm_goto_next_sign()
307 " call Dfunc("Vm_goto_next_sign()")
308
309 let curr_line_number = line(".")
310 let next_sign_line_number = s:Vm_get_next_sign_line(curr_line_number)
311
312 " call Decho("Next sign line #: " . next_sign_line_number)
313 if next_sign_line_number >= 0
314 exe ":" . next_sign_line_number
315 "call Decho("Going to Line #" . next_sign_line_number)
316 endif
317
318 " call Dret("Vm_goto_next_sign")
319 endfun
320
321 " ---------------------------------------------------------------------
322 " Vm_goto_prev_sign:
323 fun! Vm_goto_prev_sign()
324 " call Dfunc("Vm_goto_prev_sign()")
325
326 let curr_line_number = line(".")
327 let prev_sign_line_number = s:Vm_get_prev_sign_line(curr_line_number)
328 " call Decho("Previous sign line #: " . prev_sign_line_number)
329
330 if prev_sign_line_number >= 0
331 exe prev_sign_line_number
332 endif
333
334 " call Dret("Vm_goto_prev_sign")
335 endfun
336
337 " ---------------------------------------------------------------------
338 "------------------------------------------------------------------------------
339 "Add by myself
340 "Function : AutoMarkInst()
341 "Description : Auto Mark The line of inst
342 " Must set the inst name such as : abc_inst : abc
343 " Must have the plugin "visualmark"
344 "------------------------------------------------------------------------------
345 " AutoMarkInst:
346 function AutoMarkInst()
347 let line = 1
348 while line <= line("$")
349 let flag_mark = match(getline(line),"_inst")
350 if (flag_mark != -1)
351 let curr_line_number = line
352 let sign_id = s:Vm_get_sign_id_from_line(curr_line_number)
353
354 if sign_id < 0
355 let is_on = 0
356 else
357 let is_on = 1
358 endif
359
360 if (is_on != 1)
361 if !exists("b:Vm_sign_number")
362 let b:Vm_sign_number = 1
363 endif
364
365 exe 'sign define SignSymbol linehl=SignColor texthl=SignColor'
366 exe 'sign place ' . b:Vm_sign_number . ' line=' . line . ' name=SignSymbol buffer=' . winbufnr(0)
367
368 let vsn = b:Vm_sign_number
369 let b:Vm_sign_number = b:Vm_sign_number + 1
370 endif
371 endif
372 let line = line + 1
373 endwhile
374 endfunction
375 autocmd BufWinEnter *.vhd call AutoMarkInst()
376
https://files.cnblogs.com/ifys/visualmark.rar