Reply to message «Re: detecting first and last line of the current paragraph», 
sent 02:59:48 23 July 2011, Saturday
by Jose Caballero:

>         let firstline = search('^\s*$', 'bnW') + 1
>         let lastline = search('^\s*$\|\%$', 'nW', line('$'))
>         if lastline != line('$')
>                 let lastline = lastline - 1
>         endif
I guess this should be
    " If search fails firstline will be equal to 1 which is good, but
    " I am not sure whether you are aware of it
    let firstline=search('\v^\s*$', 'bnW')+1
    " "W" already forces search not to go past line('$'), so {stopline}
    " is not needed. But with `\|\%$' you can't distinguish between cases
    " 1. line("$") is a blank line and so should not be commented and
    " 2. line("$") is not a blank line
    let lastline=search('\v^\s*$', 'nW')-1
    if lastline==-1
        let lastline=line('$')
    endif

> So far, that is the best solution I have. It is quite ugly, but seems to
> work...
I do not think it is ugly. Unlike other suggested solutions that all use visual 
mode and/or :substitute your does not alter last search pattern and last visual 
area what may be annoying (especially altering last search pattern). You may 
want to see how I implemented transliteration of given area without using 
visual 
mode and/or :substitute: 
http://translit3.hg.sourceforge.net/hgweb/translit3/translit3/file/21dbcc564892/plugin/translit3.vim#l2218.
Much more code then with :normal!/:substitute, but it also works.

Original message:
> Hi,
> 
> I wanted to thank you guys for the patience, and for all the tips.
> I am quite new with Vimscript, so everything is hard to me. But that is the
> reason I keep trying, to learn and to educate myself. Sorry for bothering
> you guys.
> As I said, trying to educate myself with Vimscript, I am still trying to do
> it searching for first and last line in the block, and then iterate over
> them.
> 
> So far, that is the best solution I have. It is quite ugly, but seems to
> work...
> 
> 
> function Comment()
>         let firstline = search('^\s*$', 'bnW') + 1
>         let lastline = search('^\s*$\|\%$', 'nW', line('$'))
>         if lastline != line('$')
>                 let lastline = lastline - 1
>         endif
>         for linenum in range(firstline, lastline)
>                 let oldline = getline(linenum)
>                 let newline = '#'.oldline
>                 call setline(linenum, newline)
>         endfor
> endfunction
> 
> 
> Thanks a lot for everything.
> 
> 
> Cheers,
> Jose

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

Reply via email to