On 20-Nov-12 07:58:05 +0100, Christian Brabandt wrote:

> On Mon, November 19, 2012 22:52, So8res wrote:
>> I think functions with [range] should preserve the cursor line. Assume you
>> have:
>>
>> function RangeTest() range
>>   echo line('.')
>> endfunction
>>
>> And you put the cursor on line 2 and :%call RangeTest()
>>
>> This function will echo 1, because the cursor is moved to the beginning of
>> the range *before* entering the function.
>>
>> I was attempting to create a function which does a search-replace (:s)
>> without moving the cursor (using winsaveview() and winrestview()). It
>> turns out this is not possible, because the cursor is moved before
>> winsaveview() can be called.
>>
>> Is this the intended behavior, or is it a bug? If it's intended, is there
>> a way to save the winview before entering a function with [range]?
> 
> I also stumbled over this behaviour in my csv plugin. I think this is
> documented somewhere, but can't find it currently.

It is not (yet) documented at :help a:firstline, and "the function is expected
to take care of a range itself" would suggest that no automatic cursor placement
is done. I have also encountered this peculiarity when writing custom mappings,
too, and would be in favor of changing the behavior, as I would guess that few
to none plugins rely on that.

> What I usually do, is either surround the call to the function by
> winsaveview()/winrestview() calls. But this gets ugly when mappings
> thing so I instead simply call the function without a range but have
> the function take 2 extra arguments for the range.

I omit the range attribute on the function and explicitly pass the range
in:
#v+
    function! s:Foo( firstLine, lastLine )
        ...
    endfunction
    command! -range=% Foo call <SID>Foo(<line1>,<line2>)
#v-

-- regards, ingo

-- 
You received this message from the "vim_dev" 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

Raspunde prin e-mail lui