" ingo/subst/expr/emulation.vim: Function to emulate sub-replace-expression for recursive use. " " DEPENDENCIES: " - ingo/collection.vim autoload script " " Copyright: (C) 2014 Ingo Karkat " The VIM LICENSE applies to this script; see ':help copyright'. " " Maintainer: Ingo Karkat " " REVISION DATE REMARKS " 1.017.001 07-Mar-2014 file creation function! s:Submatch( idx ) return get(s:submatches, a:idx, '') endfunction function! s:EmulateSubmatch( originalExpr, expr, pat, sub ) let s:submatches = matchlist(a:expr, a:pat) if empty(s:submatches) let l:innerReplacement = a:originalExpr else let l:innerReplacement = eval(a:sub) endif unlet s:submatches return l:innerReplacement endfunction function! ingo#subst#expr#emulation#Substitute( expr, pat, sub, flags ) if a:sub =~# '^\\=' " Recursive use of \= is not allowed, so we need to emulate it: " matchlist() will get us the list of (sub-)matches, which we'll inject " into the passed expression via a s:Submatch() surrogate function for " submatch(). let l:emulatedSub = substitute(a:sub[2:], '\w\@" endwhile else " For a first-only replacement, just match and replace once. let s:submatches = matchlist(a:expr, a:pat) let l:innerReplacement = s:EmulateSubmatch(a:expr, a:expr, a:pat, l:emulatedSub) let l:replacement = substitute(a:expr, a:pat, escape(l:innerReplacement, '\&'), '') endif else let l:replacement = substitute(a:expr, a:pat, a:sub, a:flags) endif return l:replacement endfunction " vim: set ts=8 sts=4 sw=4 noexpandtab ff=unix fdm=syntax :