Reply to message «Aw: Re: How to execute :s command without adding the search 
pattern to the search history?», 
sent 21:08:31 13 June 2011, Monday
by lith:

> Please correct my if I'm wrong, but the use of substitute() would also
> require the use of getline(), :delete, insert() etc. Marks would get lost.
> Cursor positioning would have to be done by hand. Multi-line patterns would
> make it even worse. Or am I missing something?
Generally no, but single-line patterns are normal with getline()+setline():

    for lnr in range(a:firstline, a:lastline)
        let line=getline(lnr)
        let newline=substitute(line, pattern, replacement, flags)
        if line isnot# newline
            call setline(lnr, newline)
        endif
    endfor

Marks won't get lost in this case, though I would prefer the following:

    let s:F={}
    function s:F.histget(history)
        let r=[]
        while 1
            let histentry=histget(a:history, -1)
            if histdel(a:history, -1)
                call insert(r, histentry)
            else
                return r
            endif
        endwhile
    endfunction
    function s:F.histextend(history, histlst)
        while !empty(a:histlst)
            call histadd(a:history, remove(a:histlst, 0))
        endwhile
        return a:histlst
    endfunction
    function Replace(pat, repl, f) range
        let savedhistory=s:F.histget('search')
        let savedwinview=winsaveview()
        try
            keepjumps lockmarks
            \ execut a:firstline.','.a:lastline.'sm/'.escape(a:pat, '/').'/'
                                                   \.escape(a:repl, '/').'/'.a:f
        finally
            call s:F.histextend('search', savedhistory)
            call winrestview(savedwinview)
        endtry
    endfunction

Unlike previous one, it supports multiline patterns.

Original message:
> Am Montag, 13. Juni 2011 14:52:20 UTC+2 schrieb Charles Campbell:
> > For :s, use substitute().
> 
> Please correct my if I'm wrong, but the use of substitute() would also
> require the use of getline(), :delete, insert() etc. Marks would get lost.
> Cursor positioning would have to be done by hand. Multi-line patterns would
> make it even worse. Or am I missing something?
> 
> Regards,
> Tom

Attachment: signature.asc
Description: This is a digitally signed message part.

Reply via email to