gf (goto file) and "use lib"

122 views
Skip to first unread message

David Lewis

unread,
Sep 11, 2014, 1:18:52 PM9/11/14
to vim-...@googlegroups.com
I'm trying to find a way to expand the paths vim uses when you call gf, specifically to include paths from "use lib" statements or other such modifications to @INC within the given Perl source.  I have a fairly heavy-handed mechanism for doing this by invoking perl-vimscript but feel like there is likely an easier way that incorporates with the existing gf mechanism.

Thanks in advance for your help!

David

Benjamin Fritz

unread,
Sep 11, 2014, 6:00:32 PM9/11/14
to vim-...@googlegroups.com


On Thu, Sep 11, 2014 at 12:18 PM, David Lewis <jdavi...@gmail.com> wrote:
I'm trying to find a way to expand the paths vim uses when you call gf, specifically to include paths from "use lib" statements or other such modifications to @INC within the given Perl source.  I have a fairly heavy-handed mechanism for doing this by invoking perl-vimscript but feel like there is likely an easier way that incorporates with the existing gf mechanism.

Thanks in advance for your help!


I'd guess you can use 'includeexpr' and 'include' for this.

:help 'includeexpr' says:

Expression to be used to transform the string found with the 'include'
option to a file name.  Mostly useful to change "." to "/" for Java: >
:set includeexpr=substitute(v:fname,'\\.','/','g')
< The "v:fname" variable will be set to the file name that was detected.

Also used for the |gf| command if an unmodified file name can't be
found.  Allows doing "gf" on the name after an 'include' statement.

It looks like the default perl.vim ftplugin file already defines values for these variables to let them work with "use" or "require" statements. I don't code enough in Perl to know whether they work, but it seems like what you're trying should already be supported by default to some extent.

David Lewis

unread,
Sep 12, 2014, 12:43:02 PM9/12/14
to vim-...@googlegroups.com
Thanks for the response, Ben.  I'll see what I can do with that and post my results here.

Andy Lester

unread,
Sep 12, 2014, 12:46:12 PM9/12/14
to vim-...@googlegroups.com

On Sep 12, 2014, at 11:43 AM, David Lewis <jdavi...@gmail.com> wrote:

Thanks for the response, Ben.  I'll see what I can do with that and post my results here.

And then maybe it can be the start of an FAQ.  Mmmm, FAQ.

--
Andy Lester => www.petdance.com

David Lewis

unread,
Sep 13, 2014, 2:29:58 AM9/13/14
to vim-...@googlegroups.com
My ultimate goal is to find a way to search the current buffer for "use lib" statements and utilize/evaluate those to manipulate the search path, thereby allowing for more dynamic library inclusion.  I'm not sure what "include" is intended to do, but it is definitely not doing that.

The mechanism I developed to accomplish this is to run the following perl vimscript:

my @use_lines = grep(m/use lib /, $self->curwin->Buffer->Get(1 .. $self->curwin->Buffer->Count()));

for my $use_line(@use_lines) {
    if($use_line =~ m/FindBin/) {
        # Replace FindBin::? with appropriate overrides from the VIM API.
        my $current_file = VIM::Eval('expand("%:p:h")');
        $use_line =~ s{\$FindBin::Bin}{$current_file}g;
        $use_line =~ s{\$FindBin::RealBin}{$current_file}g;
    }
    try {
        VIM::Msg("modified use_line: $use_line") if($self->debug);
        eval($use_line);
    } catch {
        # Do nothing as this was un-eval-able
    };
}

This adds the desired library paths to Perl's @INC, and allows for interpretation of FindBin::? variables often used in 'use lib' statements.  The search path could then be updated in the same way it is being updated in perl.vim.

I'm sure there are significant security concerns due to the use of string evals in this operation, or if I'm barking up the wrong tree entirely, I would appreciate any suggestions.

Thanks again!
Reply all
Reply to author
Forward
0 new messages