Search code examples
perlvimsubroutinevim-perl

Vim plugin to show current Perl subroutine


I'm trying to make a Vim plugin that will split the window on load and simulate a info bar at the top of my terminal. I've got it sorta working but I think I've either reached limits of my knowledge of Vim syntax or there's a logic problem in my code.

The desired effect would be to do a reverse search for any declaration of a Perl subroutine form my current location in the active buffer and display the line in the top buffer. I'm also trying to make it skip that buffer when I switch buffers with Ctrl-R. My attempt at that so far can be seen in the mess of nested if statements.

Anyway, here's the code. I would greatly appreciate feedback from anyone.

let s:current_function_bufname = 'Current\ Function\/Subroutine'
function! s:get_current_function_name(no_echo)
    let lnum = line(".")
    let col = col(".")
    if a:no_echo
        let s:current_function_name = getline(search("^[^s]*sub .*$", 'bW'))
    else
        echohl ModeMsg
        echo getline(search("^[^s]*sub .*$", 'bW'))
        "echo getline(search("^[^ \t#/]\\{2}.*[^:]\s*$", 'bW'))
        echohl None
    endif
endfunction

let s:previous_winbufnr = 1
let s:current_function_name = ''
let s:current_function_buffer_created = 0
let s:current_function_bufnr = 2
function! s:show_current_function()
    let total_buffers = winnr('$')
    let current_winbufnr = winnr()
    if s:previous_winbufnr != current_winbufnr
        if bufname(current_winbufnr) == s:current_function_bufname
            if s:previous_winbufnr < current_winbufnr
                let i = current_winbufnr + 1
                if i > total_buffers
                    let i = 1
                endif
                if i == s:current_function_bufnr
                    let i = i + 1
                endif
                if i > total buffers
                    let i = 1
                endif
                exec i.'wincmd w'
            else
                let i = current_winbufnr - 1
                if i < 1
                    let i = total_buffers
                endif
                if i == s:current_function_bufnr
                    let i = i - 1
                endif
                if i < 1
                    let i = total_buffers
                endif
                try
                    exec i.'wincmd w'
                finally
                    exec total_buffers.'wincmd w'
                endtry
            endif
        endif
        let s:previous_winbufnr = current_winbufnr
        return 1
    endif

    if s:current_function_buffer_created == 0
        exec 'top 1 split '.s:current_function_bufname
        call s:set_as_scratch_buffer()
        let s:current_function_buffer_created = 1
        let s:current_function_bufnr = winnr()
    endif

    call s:activate_buffer_by_name(s:current_function_bufname)
    setlocal modifiable

    call s:get_current_function_name(1)
    call setline(1, s:current_function_name)

    setlocal nomodifiable
    call s:activate_buffer_by_name(bufname(current_winbufnr))
endfunction

function! s:set_as_scratch_buffer()
   setlocal noswapfile
   setlocal nomodifiable
   setlocal bufhidden=delete
   setlocal buftype=nofile
   setlocal nobuflisted
   setlocal nonumber
   setlocal nowrap
   setlocal cursorline
endfunction

function! s:activate_buffer_by_name(name)
   for i in range(1, winnr('$'))
      let name = bufname(winbufnr(i))
      let full_name = fnamemodify(bufname(winbufnr(i)), ':p')
      if name == a:name || full_name == a:name
         exec i.'wincmd w'
         return 1
      endif
   endfor
   return 0
endfunction

set laststatus=2
autocmd! CursorMoved,CursorMovedI,BufWinEnter * call s:show_current_function()

Similar to the question VIM: display custom reference bar on top of window and the Vim Tips wiki page Show current function name in C programs.


Solution

  • After reviewing the code on http://blogs.perl.org/users/ovid/2011/01/show-perl-subname-in-vim-statusline.html suggested by @Prakash I decided to share my improvements to the script.

    I played with the code one his site for a little while and made an improvement. This will display N/A if you're outside the subroutine. If you have a history of your closing brace indent not matching your sub declaration indent you may want to alter the $indent portion. Have fun!

    .vimrc:

    syntax on
    setlocal laststatus=2
    setlocal statusline=\ %{HasPaste()}%F%m%r%h\ %w\ \ CWD:\ %r%{CurDir()}%h\ \ \ Position:\ %p%%\ %l/%L,%c
    if has("autocmd")
        autocmd BufReadPost * if &syntax == 'perl' | source ~/.vim/perl_current_subroutine | endif
    endif
    

    .vim/perl_current_subroutine:

    if ! exists("b:did_perl_statusline") && &syntax == 'perl'
        setlocal statusline+=%(\ \ \ Subroutine:\ %{StatusLineIndexLine()}%)
        let b:did_perl_statusline = 1
    endif
    
    if has('perl')
    perl << EOP
        use strict;
        sub current_perl_subroutine {
            my $curwin = $main::curwin;
            my $curbuf = $main::curbuf;
            my $sub_name = 'N/A';
    
            #search up from cursor line to find a declaration of a subroutine
            my $line_number = ($curwin->Cursor)[0];
            my $line = $curbuf->Get($line_number);
            my $indent = '';
            if ($line !~ /^(\s*)sub\s+(\w+)\b/) {
                $line = $curbuf->Get($line_number) while ($line_number-- > 0 && $line !~ /^(\s*)sub\s+(\w+)\b/);
                ($indent, $sub_name) = ($1, $2);
            } else {
                $line_number--;
                ($indent, $sub_name) = ($1, $2);
            }
    
            #if found, (try to) find the end of the subroutine
            if ($sub_name ne 'N/A') {
                my $end = $curbuf->Count();
                $line = $curbuf->Get($line_number);
                if ($line !~ /}\s*$/) {
                    $line = $curbuf->Get($line_number) while ($line_number++ < $end && $line !~ /^($indent)?}\s*$/);
                    $line_number--;
                }
                $sub_name = 'N/A' if ($line =~ /^($indent)?}\s*$/ && ($curwin->Cursor)[0] > $line_number);
            }
    
            VIM::DoCommand("let current_perl_subroutine_name='$sub_name'");
        }
    EOP
    
        function! StatusLineIndexLine()
            perl current_perl_subroutine()
            return current_perl_subroutine_name
        endfunction
    endif