1

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. Here one can see the little empty space in purple

*"subroutine" is the name for Fortran's subprograms.

0

1 Answer 1

2

'syntax' is an option which is local to a buffer, I don't think you should test it inside your vimrc but inside your function SubName(), which you already did. So, you probably don't need the test around set statusline+=%8*\ %{SubName()}\.

Inside your vimrc, you could simply write this:

function! SubName()
   return &syntax !=# 'fortran' ? '' : ' ' . matchstr(getline(search('subroutine ', 'bcnW')), 'subroutine \zs\w\+') . ' '
endfunction

set statusline+=%8*%{SubName()}

The reason why it didn't work is because during the startup, when Vim sources your vimrc, it's not in the context of a fortran buffer. The test fails and no %{SubName()} item is added to the statusline.

The test has to be performed in the context of the current buffer, inside the function.


The reason why, when opening a non-fortran file, you get a small empty zone in your statusline with color highlight %8 is because you didn't move the spaces around the item from your 'statusline' option to your function.

In your previous code, you had this line:

set statusline+=%8*\ %{SubName()}\ 

The backslash and space before and after the %{SubName()} item shouldn't be there, they should be inside your function where the syntax test is realised.

In the code I wrote earlier, there's this line:

   return &syntax !=# 'fortran' ? '' : ' ' . matchstr(getline(search('subroutine ', 'bcnW')), 'subroutine \zs\w\+') . ' '

Before and after the matchstr() function, there's ' ' . and . ' '. These are the original spaces which will be added around your subroutine name, but only if you're inside a fortran file.

4
  • Does your new syntax of the SubName function changes anything ? I tried it but it does not change much (except the size of my statusline zone). Taking off the test around set statusline did the trick, thanks, but now I have a little empty zone with the %8 color in my statusline when opening a non-fortran file. It would be even better if I could get rid of it.
    – Feffe
    May 24, 2016 at 9:53
  • @Feffe No it doesn't change anything, it's just shorter. The only difference is a space before and after the name of the subroutine inside the function, because I saw there were two spaces inside your code to separate the name from the rest of the information. I don't have a little empty zone with the %8 color in my statusline when opening a non-fortran file. I'm sorry but I don't understand why you have this zone. It shouldn't be there if you move the spaces inside the function, where the test is realised. Could you give me a link of your complete statusline configuration? May 24, 2016 at 10:05
  • I added it to my question, tell me if you'd rather have a text file.
    – Feffe
    May 24, 2016 at 10:19
  • I should have copied your version of the set statusline. Thank you for your patience and explanations.
    – Feffe
    May 24, 2016 at 10:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.