I would like to get not the first, but the second occurrence of my
I could not find any flag or option in
search() documentation that could help me do that.
I really liked my flags, especially '
b' to search backwards and '
n' to avoid cursor movement, so I wanted to see if there was a way to reach the second occurrence of my pattern with this function.
I wanted the question to be as short and general as possible, here's a bit more context : I'm coding in Fortran and I want the name of the subroutine I'm currently in to be displayed in my statusline. I therefore ended up with a little function (thanks to this post) :
function! SubName() abort let n_line = search('subroutine ', 'bcnW') " find previous occurrence of 'subroutine' return matchstr(getline(n_line), 'subroutine \zs\w\+') " extract the word right after 'subroutine', which should be its name endfunction
However, I sometimes write the word 'subroutine' in my code comments (for instance,
This subroutine really useful is incomprehensible.). In that case, right after the comment line, the function will extract "really" thinking it's my subroutine's name.
So I came up with a condition to detect comments (if the beginning of the line is a '!' or a 'c' character).
But if this condition is true, I need to do my
search() again, this time up to the second match, so that the 'subroutine' pattern that is found is really the declaration of the subroutine, and not just one of my comments.
A generic fortran code could be
subroutine mySubroutine ! This is an useful subroutine yeah. ! Stuff happens end
Here, I would like my function to extract "mySubroutine" instead of "yeah", even if my cursor is at the bottom.