I have a function that detects the name of the subroutine* my cursor is in, thanks to this answer.
function! SubName() abort
if &syntax !='fortran'
return ""
endif
let prev_sub_line_num = search('subroutine ', 'bcnW')
return matchstr(getline(prev_sub_line_num), 'subroutine \zs\w\+')
endfunction
It is used in my statusline as follows :
if &syntax== 'fortran'
set statusline+=%8*\ %{SubName()}\
endif
It works well, excepted that I have to source my .vimrc
file upon opening VIM every time I want it to work. I would like some sort of 'autocmd' that would save me this sourcing at each session I'm starting -- and also understanding what's happening !
(I naively tried autocmd BufWritePre,BufRead *.f* call SubName()
but it does not seem to change anything).
Edit
user9433424 gave me an explanation as well as a solution through taking away the test around the set statusline
command.
It works, but now when opening a non-fortran file I get a small empty zone in my statusline with color highlight %8
... And it's a little ugly. I understand why but I'd love to get rid of it !
(Maybe I could just add a else
case in my SubName
function like :
else
return "You're awesome !"
endif
but it was not really my initial plan !)
Edit II
As asked by user9433424, here is the part of my .vimrc
that concerns my statusline.
*"subroutine" is the name for Fortran's subprograms.