I am trying to match a block of fortran, say,
foo i()
...
End foo i
with
autocmd CursorMovedI *.f90 :call FixName("foo")
where FixName
is:
" FixName: Change Subprogram name {{{1
function! FixName(arg)
let [buf, l, c, off] = getpos('.')
call cursor([1, 1, 0])
let lnum = search('\v\c^\s*' . a:arg . '\s+', 'cnW')
if !lnum
call cursor(l, c, off)
return
endif
let parts = matchlist(getline(lnum), '\v\c^\s*' . a:arg . '\s+(\S*)\s*$')
if len(parts) < 2
call cursor(l, c, off)
return
endif
let lnum = search('\v\c^\s*End\s*' . a:arg . '\s+', 'cnW')
call cursor(l, c, off)
if !lnum
return
endif
call setline(lnum, substitute(getline(lnum), '\v\c^\s*End\s*' . a:arg . '\s+\zs.*', parts[1], ''))
endfunction
The problem is, with this FixName, I am getting anything after the foo. So, if I have, foo bar(anything)
, I am ending up with End foo bar(anything)
. But, I want only the bar (i.e. before the "(").
I have tried to change the last line as:
call setline(lnum, substitute(getline(lnum), '\v\c^\s*End\s*' . a:arg . '\s+\zs.*[?=\(]', parts[1], ''))
but obviously that's not the correct way. How should I correct it?
'\s+(\S*)\(.*\)\s*$'
?\s+(.*)(?=\()
. But, vim is complaining aboutE866: misplaced ?
let parts = matchlist(getline(lnum), '\v\c^\s*' . a:arg . '\s+(\S*)\(.*\)\s*$')
. As for what your Python code does::h zero-width
.