2011/7/23 Ben Schmidt <[email protected]>

> 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,
>>
>
> You don't need to apologies for bothering us. If we didn't enjoy helping
> people with Vim, we wouldn't be on this mailing list. It's also clear
> that you've put in some effort yourself into solving your problems, and
> aren't just using us as a first response. Anyone with that kind of
> attitude will be warmly welcomed here, and have their questions gladly
> received (and if they're not answered, it's probably because we don't
> know the answer...).
>
>

Thanks a lot for your kind comments.
So here is an implementation I am minimally satisfied with (can be improved,
but for the time being I am happy with it).
It would have been impossible without your help.


function Comment(...) range
        "
        " Function to comment-out and uncomment-out blocks of code
        "
        " If a range is passed, all lines in it are used.
        " Otherwise, the first and last line of the current paragraph
        " are calculated.
        "
        " The commenting string can be passed as argument.
        " # is used as default otherwise.
        "
        " Once the region and commenting string are determined,
        " it checks if all lines in that region already starts
        " with that string. In that case it is assumed the
        " desired operation is to uncomment the block.
        " If at least one line is not already commented it is
        " assumed the desired operation is to comment the block.
        "

        " --------------------------------------------------
        "  determining range
        " --------------------------------------------------

        if a:firstline == a:lastline
                " no range has been provide, so ...
                " searching for first and last line of the current block.

                " searching backwards for the first blank line, and ading 1
                let firstline = search('\v^\s*$', 'bnW') + 1

                " searching forward for the first blank line, and
substracting 1
                let lastline = search('\v^\s*$', 'nW') - 1
                if lastline == -1
                        " we hitted the end of the file
                        let lastline = line('$')
                endif
        else
                " the range was provided
                let firstline = a:firstline
                let lastline = a:lastline
        endif

        " --------------------------------------------------
        "  determining commenting string
        " --------------------------------------------------

        let comstring = a:0 == 1 ? a:1 : '#'

        " --------------------------------------------------
        "  checking if the region is already commented
        " --------------------------------------------------

        let commented = 1
        for linenum in range(firstline, lastline)
                let line = getline(linenum)
                if match(line, comstring) != 0
                        " as soon as a line not starting
                        " with the commenting string is found
                        " is it understood the region is NOT commented
                        let commented = 0
                endif
        endfor

        " --------------------------------------------------
        "  operations
        " --------------------------------------------------

        for linenum in range(firstline, lastline)
                let oldline = getline(linenum)
                if commented == 1
                        " uncommenting-out ...
                        " the commenting string is substituted by ''
                        let newline = substitute(oldline, comstring, '', '')

                else
                        " commenting-out ...
                        " the commenting string is added to the line
                        let newline = comstring.oldline
                endif
                call setline(linenum, newline)
        endfor
endfunction

-- 
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

Reply via email to