- 论坛徽章:
- 0
|
在vim里编辑perl代码时,只要将光标移到模块名上,然后敲入':gm',就能立刻导航至那个模块。
特性:
1 使用@INC变量查找模块;
2 如果找到的模块只有一个,就会立刻跳至那个模块;
3 如果找到的模块不止一个(多个版本),会显示一个可供选择的列表(就像命令补全一样);
4 如果没有找到模块,则会给出高亮提示信息"Module '$module' not found";
函数代码如下:
let g:perl_path_to = {}
function! GotoModule(module)
let files = []
if !has_key(g:perl_path_to, a:module)
let g:perl_path_to[a:module] = []
let lib = split(system("perl -le 'print join $/ => @INC'"), "\n")
let module = substitute(a:module, '::', '/', 'g') . '.pm'
for path in lib
let path = path . '/' . module
if filereadable(path)
let g:perl_path_to[a:module] = g:perl_path_to[a:module] + [ path ]
endif
endfor
endif
let paths = g:perl_path_to[a:module]
if empty(paths)
echomsg("Module '".a:module."' not found")
else
let file = PickFromList('file', paths)
endif
execute "edit " . file
endfunction
function! PickFromList( name, list, ... )
let forcelist = a:0 && a:1 ? 1 : 0
if 1 == len(a:list) && !forcelist
let choice = 0
else
let lines = [ 'Choose a '. a:name . ':' ]
\ + map(range(1, len(a:list)), 'v:val .": ". a:list[v:val - 1]')
let choice = inputlist(lines)
if choice > 0 && choice <= len(a:list)
let choice = choice - 1
else
let choice = choice - 1
endif
end
return a:list[choice]
endfunction |
使用方法:
在.vimrc里加入:
" only works for Perl
au! FileType perl :noremap <buffer> :gm :call GotoModule(expand('<cword>'))<cr>
" make sure we pick up the colon as part of our keyword
autocmd FileType perl setlocal iskeyword+=:
" don't kill 'undo' in other buffers
set hidden
使用此方法可以提高编写较复杂程序时的效率,平常写脚本的时候也可以随时很方便地用它查看核心模块的代码。
原文:http://use.perl.org/~Ovid/journal/36602
(作者Ovid是O‘relly Perl Hacks的主要作者)
[ 本帖最后由 Nosferatu 于 2008-6-9 21:59 编辑 ] |
|