Vorrei usare una funzione che ha uno stato e chiamare questa funzione da% s. Qualcosa di simile:
" untested code
function! InitRotateSubst()
let s:rs_idx = 0
endfunction
function! RotateSubst(list)
let res = a:list[s:rs_idx]
let s:rs_idx += 1
if s:rs_idx == len(a:list)
let s:rs_idx = 0
endif
return res
endfunction
e utilizzarli con:
:call InitRotateSubst()
:%s/foo/\=RotateSubst(['bar', 'bar', 'baz', 'baz'])/
L'invito ai due comandi potrebbe essere incapsulato in un unico comando, se lo si desidera.
EDIT: Ecco una versione integrata come un comando che:
- accetta il maggior numero di sostituzioni come vogliamo, tutte le sostituzioni deve essere separato con il separatore caratteri;
- supporta i riferimenti a ritroso;
- può sostituire solo le prime occorrenze N, n == il numero di sostituzioni specificato se la chiamata comando viene sbattuto (con un!)
- non supporta bandiere soliti like g, i (
:h :s_flags
) - per questo, dovremmo ad esempio imporre la chiamata al comando per finire sempre con un/(o qualsiasi altro carattere separatore), se l'ultimo testo non viene interpretato come flag.
Ecco la definizione del comando:
:command! -bang -nargs=1 -range RotateSubstitute <line1>,<line2>call s:RotateSubstitute("<bang>", <f-args>)
function! s:RotateSubstitute(bang, repl_arg) range
let do_loop = a:bang != "!"
" echom "do_loop=".do_loop." -> ".a:bang
" reset internal state
let s:rs_idx = 0
" obtain the separator character
let sep = a:repl_arg[0]
" obtain all fields in the initial command
let fields = split(a:repl_arg, sep)
" prepare all the backreferences
let replacements = fields[1:]
let max_back_ref = 0
for r in replacements
let s = substitute(r, '.\{-}\(\\\d\+\)', '\1', 'g')
" echo "s->".s
let ls = split(s, '\\')
for d in ls
let br = matchstr(d, '\d\+')
" echo '##'.(br+0).'##'.type(0) ." ~~ " . type(br+0)
if !empty(br) && (0+br) > max_back_ref
let max_back_ref = br
endif
endfor
endfor
" echo "max back-ref=".max_back_ref
let sm = ''
for i in range(0, max_back_ref)
let sm .= ','. 'submatch('.i.')'
" call add(sm,)
endfor
" build the action to execute
let action = '\=s:DoRotateSubst('.do_loop.',' . string(replacements) . sm .')'
" prepare the :substitute command
let args = [fields[0], action ]
let cmd = a:firstline . ',' . a:lastline . 's' . sep . join(args, sep)
" echom cmd
" and run it
exe cmd
endfunction
function! s:DoRotateSubst(do_loop, list, replaced, ...)
" echom string(a:000)
if ! a:do_loop && s:rs_idx == len(a:list)
return a:replaced
else
let res0 = a:list[s:rs_idx]
let s:rs_idx += 1
if a:do_loop && s:rs_idx == len(a:list)
let s:rs_idx = 0
endif
let res = ''
while strlen(res0)
let ml = matchlist(res0, '\(.\{-}\)\(\\\d\+\)\(.*\)')
let res .= ml[1]
let ref = eval(substitute(ml[2], '\\\(\d\+\)', 'a:\1', ''))
let res .= ref
let res0 = ml[3]
endwhile
return res
endif
endfunction
che potrebbe essere utilizzato in questo modo:
:%RotateSubstitute#foo#bar#bar#baz#baz#
o addirittura, considerando il testo iniziale:
AfooZ
BfooE
CfooR
DfooT
comando
%RotateSubstitute/\(.\)foo\(.\)/\2bar\1/\1bar\2/
produrrebbe:
ZbarA
BbarE
RbarC
DbarT
Sembra che manchi un 'n' prima della tua finale' .' – Rich