Match parenthesised block using regular expressions in vim

This is not possible with vim regular expressions (as language that allows such nested constructs is not regular), but is possible with ‘regular’ expressions provided by perl (as well as by other languages I do not know enough to be sure) and perl can be used from inside vim. I don’t like vim-perl bindings (because it is very limited), but if you know all cases that should work, then you could use recursion feature of perl regular expressions (requires newer perl, I have 5.12*):

perl VIM::Msg($+{"outer"}) if $curbuf->Get(3) =~ /someKeyword\((?'outer'(?'inner'"(?:\\.|[^"])*"|'(?:[^']|'')*'|[^()]*|\((?P>inner)*\))*)\)/

Note that if can avoid such regular expressions, you should do it (because you depend on re compiler too much), so I suggest to use vim motions directly:

let s:reply=""
function! SetReplyToKeywordArgs(...)
    let [sline, scol]=getpos("'[")[1:2]
    let [eline, ecol]=getpos("']")[1:2]
    let lchar=len(matchstr(getline(eline), '\%'.ecol.'c.'))
    if lchar>1
        let ecol+=lchar-1
    endif
    let text=[]
    let ellcol=col([eline, '$'])
    let slinestr=getline(sline)
    if sline==eline
        if ecol>=ellcol
            call extend(text, [slinestr[(scol-1):], ""])
        else
            call add(text, slinestr[(scol-1):(ecol-1)])
        endif
    else
        call add(text, slinestr[(scol-1):])
        let elinestr=getline(eline)
        if (eline-sline)>1
            call extend(text, getline(sline+1, eline-1))
        endif
        if ecol<ellcol
            call add(text, elinestr[:(ecol-1)])
        else
            call extend(text, [elinestr, ""])
        endif
    endif
    let s:reply=join(text, "\n")
endfunction
function! GetKeywordArgs()
    let winview=winsaveview()
    keepjumps call search('someKeyword', 'e')
    setlocal operatorfunc=SetReplyToKeywordArgs
    keepjumps normal! f(g@i(
    call winrestview(winview)
    return s:reply
endfunction

You can use something like

let savedureg=@"
let saved0reg=@0
keepjumps normal! f(yi(
let s:reply=@"
let @"=savedureg
let @0=saved0reg

instead of operatorfunc to save and restore registers, but the above code leaves all registers and marks untouched, what I can’t guarantee with saved* stuff. It also guarantees that if you remove join() around text, you will save information about the location of NULLs (if you care about them, of course). It is not possible with registers variant.

Leave a Comment