I am currently coding fortran, and I have huge "subprograms" (called "subroutine" in fortran). So I would like the name of the current subroutine (the subprogram my cursor is currently in) to be displayed in my statusline.
This is what a subroutine looks like in fortran (this should make my trick clearer):
subroutine my_subprogram(args)
stuff going on
end
I found the following trick, currently in my .vimrc file.
function! SubName()
let current_line = line(".") " I store the current line number
?subroutine " searches the previous occurence of "subroutine"
let title_line = line(".") " stores the line where the subroutine title is
execute ":".title_line " go to the line where the title is
normal! eeb " put the cursor on the 2nd word : "my_subprogram"
let sub_name = expand("<cword>") " extract the subroutine name in a variable
execute ":".current_line " come back to the initial position
return sub_name
endfunction
Then I put the function in my statusline and it works almost fine : the name is displayed but... I cant do /
searches anymore ! It highlights found text but I can't scroll to the next occurences of the pattern I'm looking for.
I guess this might be because the function is permanently called and thus Vim permanently performs researches because of the ?subroutine
line.
(This is my first time trying to vimscript, so there are probably some ugly things there that you may want to correct -- please do not hesitate !)