diff --git a/autoload/ingo/subst.vim b/autoload/ingo/subst.vim index 88aac49..0c14b04 100644 --- a/autoload/ingo/subst.vim +++ b/autoload/ingo/subst.vim @@ -1,6 +1,8 @@ " ingo/subst.vim: Functions for substitutions. " " DEPENDENCIES: +" - ingo/format.vim autoload script +" - ingo/subst/replacement.vim autoload script " " Copyright: (C) 2013-2018 Ingo Karkat " The VIM LICENSE applies to this script; see ':help copyright'. @@ -142,7 +144,8 @@ function! ingo#subst#Indexed( expr, pattern, replacement, indices, ... ) "* INPUTS: " a:expr Text to be transformed. " a:pattern Regular expression. -" a:replacement Replacement. |sub-replace-expression| is not supported. +" a:replacement Replacement. Handles |sub-replace-expression|, & and \0, +" \1 .. \9, and \r\n\t\b (but not \u, \U, etc.) " a:indices List of 0-based indices whose corresponding matches are " replaced. A String value of "g" replaces globally, just like " substitute(..., 'g'). @@ -167,7 +170,7 @@ endfunction function! s:IndexReplacer( context ) let a:context.matchCnt += 1 execute 'let l:isSelected = index(a:context.indices, a:context.matchCnt - 1)' (a:context.isInvert ? '==' : '!=') '-1' - return (l:isSelected ? a:context.replacement : submatch(0)) + return ingo#subst#replacement#DefaultReplacementOnPredicate(l:isSelected, a:context) endfunction let &cpo = s:save_cpo diff --git a/tests/subst/t1000-Indexed.vim b/tests/subst/t1000-Indexed.vim index 4a24e7a..36b4eb3 100644 --- a/tests/subst/t1000-Indexed.vim +++ b/tests/subst/t1000-Indexed.vim @@ -1,7 +1,7 @@ " Test substitution of indexed matches. call vimtest#StartTap() -call vimtap#Plan(9) +call vimtap#Plan(13) call vimtap#Is(ingo#subst#Indexed('foobar is loosie origon', 'o', 'X', 'g'), 'fXXbar is lXXsie XrigXn', 'global substitution') call vimtap#Is(ingo#subst#Indexed('foobar is loosie origon', 'o', 'X', [3]), 'foobar is loXsie origon', 'substitute single index') @@ -14,4 +14,9 @@ call vimtap#Is(ingo#subst#Indexed('foobar is loosie origon', 'o', 'X', [-1]), 'f call vimtap#Is(ingo#subst#Indexed('foobar is loosie origon', 'o', 'X', [2, 3, 4], 0), 'foobar is lXXsie Xrigon', 'substitute without inversion') call vimtap#Is(ingo#subst#Indexed('foobar is loosie origon', 'o', 'X', [2, 3, 4], 1), 'fXXbar is loosie origXn', 'substitute with inversion') +call vimtap#Is(ingo#subst#Indexed('foobar is loosie origon', 'o', '&&', [0, 1, 2, 3, 5]), 'foooobar is loooosie origoon', 'substitution with &') +call vimtap#Is(ingo#subst#Indexed('foobar is loosie origon', 'o', '\t', [1, 3]), "fo\tbar is lo\tsie origon", 'substitution with \t') +call vimtap#Is(ingo#subst#Indexed('foobar is loosie origon', '\(.\)\(oo\)\(.\)', '\3\2\1', [1]), 'foobar is soolie origon', 'substitution with capture groups') +call vimtap#Is(ingo#subst#Indexed('foobar is loosie origon', 'o', '\=toupper(repeat(submatch(0), 3))', [1, 3]), 'foOOObar is loOOOsie origon', 'substitution with sub-replace-expression') + call vimtest#Quit()