On Jul 27, 11:18 pm, Reckoner <[email protected]> wrote:
> Thanks! Big help!
>
Excellent.


> Is there a way to put this on the statusline?
>
Undoubtedly! This is Vim, after all!

Of course, this is a bit more complicated as you want to ensure that
it only displays a match if you are actually within a class. My
previous method involved some user intelligence. For instance, if you
are at the top of a Python file, you don't want to search backwards
and find the last class definition in the file, the user calling the
mapping would know this. Also if you are in a global function beneath
a class definition, you wouldn't want that class definition being
displayed as this would be misleading.

This sounded like an interesting challenge to me and I'd never played
with the statusline before so I delved into the help-files and off I
went. This is what I came up with in a few minutes:

function! GetLastClass()
    let l:retval = "[No class]"

    let l:last_line_declaring_a_class = search('^\s*class', 'bnW')
    let l:last_line_starting_with_a_word_other_than_class = search('^\
(\<\)\...@=\(class\)\...@!', 'bnW')

    if l:last_line_starting_with_a_word_other_than_class <
l:last_line_declaring_a_class
        let l:retval = getline(l:last_line_declaring_a_class)
    endif

    return l:retval
endfunction

setlocal statusline=[%n]\ %{GetLastClass()}\ %<%f%h%m%r%=%b\ 0x%B\ \
%l,%c%V\ %P

This is in my .vim/ftplugin/python.vim.

There are some obvious shortcomings around nested classes but I shall
leave those as an exercise for the reader. I reckon any Python
developer can write Vimscript.

Here are the relevant help pages, which should explain how this little
lot works.

:he 'statusline'
:he /multi                " \...@= and \...@! atoms were not obvious to me
at first glance
:he /ordinary-atom        "For \< . In fact the whole of pattern.txt
is essential reading for any Vim user.
:he :function
:he :endfunction
:he :getline
:he :let
:he :return
:he :search
:he :setlocal
:he ftplugin
:he local-variable

> thanks again.
>
You're welcome. I hope this satisfies your requirements. If you solve
the problems with nested classes, I would very much like to see the
solution.

Cheers;

--paj
--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply via email to