open module under cursor in vim

Inspired by http://www.slideshare.net/c9s/perlhacksonvim I wrote (well ... copied for the larger part) a script to open the Module currently under the cursor in vim.

Typing \fm will lookup the first Module found in available Perl library paths (plus current working directoy . '/lib')

I did search for some time and read a bit about ctags and pltags but ended up confused. add this to your vimrc


""""""""""""""""""""""""""""""""""""
" find module in perl INC and edit "
""""""""""""""""""""""""""""""""""""
function! GetCursorModuleName()
    let cw = substitute( expand(""), '.\{-}\(\(\w\+\)\(::\w\+\)*\).*$', '\1', '' )
    return cw
endfunction

function! TranslateModuleName(n)
    return substitute( a:n, '::', '/', 'g' ) . '.pm'
endfunction

function! GetPerlLibPaths()
    let out = system('perl -e ''print join "\n", @INC''')
    let paths = split( out, "\n" )
    return paths
endfunction

function! FindModuleFileInPaths()
    let paths = [ 'lib' ] + GetPerlLibPaths()
    let fname = TranslateModuleName( GetCursorModuleName() )

    for p in paths
        let f = p . '/' . fname
        if filereadable(f)
            exec "edit " . f
            return 1
        endif
    endfor

    echo "File not found: " . fname
endfunction

nmap fm :call FindModuleFileInPaths()

CAVEATS


  • returns only the first found module
  • "local" modules not found unless current working directory has the lib/ dir as child

8 Comments

If you're going to shell out anyway, why not use "perldoc -l" to do the finding for you?

"perldoc -l Test::More" returns "/opt/local/lib/perl5/5.12.4/Test/More.pm" on my machine, for example.

I believe there's something like this in vim-perl already, but if not, I should add it.

https://github.com/petdance/vim-perl

It seems to me using a function call at all is much too complicated:

au FileType perl command! -nargs=1 PerlModuleSource \
    :tabnew `perldoc -lm <args>`
au FileType perl setlocal iskeyword+=:                                                                                       
au FileType perl noremap <leader>P :PerlModuleSource <cword><cr>zR<cr>  

I use gf and it works perfectly. I only have this in my .vimrc:

set path+=$PWD/lib

I used to have this:

set includeexpr=substitute(substitute(v:fname,'::','/','g'),'$','.pm','')

but it works without it too.

Consider using perl-support plugin by Prof. Fritz Mehner.

http://www.vim.org/scripts/script.php?script_id=556

Under the hood, it adds many features including setting 'path' option to contain all the perl lib directories and also changing the iskeyword (which determines how a word looks like). By doing:


set iskeyword+=:

you make a module name to be a single word which means running one of the:

gf
CTRL W f
CTRL W gf
...

with the cursor anywhere on Data::Dumper will jump straight into the Dumper.pm file)
I also use some other nice plugins like:
vim-commentary - http://www.vim.org/scripts/script.php?script_id=556

taglist - http://www.vim.org/scripts/script.php?script_id=273

Check them out. You may find them very useful.

davewood:

This is your blog; you can fix up your own comments at will.

Leave a comment

About davewood

user-pic I like Toast.