A piggy bank of commands, fixes, succinct reviews, some mini articles and technical opinions from a (mostly) Perl developer.

Perl sub name in vi status line

" Thanks Ovid!
" http://blogs.perl.org/users/ovid/2011/01/show-perl-subname-in-vim-statusline.html


:set laststatus=2


if ! exists("g:did_perl_statusline")
    setlocal statusline+=%(\ %{StatusLineIndexLine()}%)
    setlocal statusline+=%=
    setlocal statusline+=%f\ 
    setlocal statusline+=%P
    let g:did_perl_statusline = 1
endif


if has( 'perl' )
perl << EOP
    use strict;
    sub current_sub {

        my $curwin = $main::curwin;
        my $curbuf = $main::curbuf;


        my @document = map { $curbuf->Get($_) } 0 .. $curbuf->Count;
        my ( $line_number, $column  ) = $curwin->Cursor;


        my $sub_name;
        # for modules, display the current sub name
        for my $i ( reverse ( 1 .. $line_number  -1 ) ) {
            my $line = $document[$i];
            if ( $line =~ /^\s*sub\s+(\w+)\b/ ) {
                $sub_name = $1;
                last;
            }
        }
        # for templates, display the current block starting line
        if (not $sub_name) {
            for my $i ( reverse ( 1 .. $line_number  -1 ) ) {
                my $line = $document[$i];
                # if ($input_state eq 'next_disc_at_station') {
                if ( $line =~ /^}/ ) {
                    # we're below a block
                    last;
                }
                elsif ( $line =~ /^(\S.+{)\s*$/ ) {
                    $sub_name = $1;
                    $sub_name =~ s/'/''/g; #' # escape single quotes for vim function
                    last;
                }
            }
        }
        # TODO:
        # * Reset sub name if we're out of the sub (check for closing bracket: })
        # * Search upwards vertically in the same column for nested blocks
        $sub_name ||= '..';
        VIM::DoCommand "let subName='$line_number: $sub_name'";
    }
EOP


function! StatusLineIndexLine()
  perl current_sub()
  return subName
endfunction
endif