Strange undo behaviour when calling a function

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Strange undo behaviour when calling a function

Sandro Bosio

Hallo,


I have a function whose purpose is to format a paragraph in a latex  
file. If I undo the call to the function, it does the undo properly,  
but it jumps to the beginning of the file.

The following stripped version of the function shows the same behaviour.

" ******************************************************
function! TestFunction()

         " Goes to beginning of file and writes a special terminator  
string
         exe ':1'
         exe ':s/.*/\r&/'
         exe ':1'
         exe ':normal iFMT_LTX_TERM'

         " Goes to end of file and writes a special terminator string
         exe ':normal G'
         exe ':s/.*/&\r/'
         exe ':normal G'
         exe ':normal iFMT_LTX_TERM'

         "does something
         "...

         " Removes special terminators
         exe ':1'
         exe ':d'

         exe ':normal G'
         exe ':d'
endfunction

Note that inverting the beginning and ending insertion/deletion does  
not change the overall effect. Undo always jumps to the beginning of  
the file. But if you remve the insertion/deletion at the beginning of  
the file, then undo jumps at the end of the file.

I tried introducing and deleting fake text where the cursor is  
supposed to be at the beginning and/or at the end of the script, with  
the hope that the undo would undo that as last
operation. But it doesn't change anything, the cursor still goes to  
the beginning of the file.

I simply want the undo to jump at the line where the fuction was  
called. Is this something related to my version (see below) or not?  
Or it simply should be like that, and there's something I don't get?

Best regards,
Sandro Bosio

VIM - Vi IMproved 7.1 (2007 May 12, compiled Sep 16 2008 18:07:41)
MacOS X (unix) version
Included patches: 1-2

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

RE: Strange undo behaviour when calling a function

JohnBeckett

Sandro Bosio wrote:
> I have a function whose purpose is to format a paragraph in a
> latex file. If I undo the call to the function, it does the
> undo properly, but it jumps to the beginning of the file.

Can we try a simpler approach with the function first? I believe the following is
the same as what you posted. If you try this, exactly what is the problem?

function! TestFunction()
  " Insert terminator at beginning and end of buffer.
  0put ='FMT_LTX_TERM'
  $put ='FMT_LTX_TERM'
  " ...
  " Remove terminators.
  1d
  $d
endfunction

John


--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Strange undo behaviour when calling a function

Sandro Bosio

Well, first let me thank you for this already useful scripting lesson.

Well, the problem is that if I am, say, on line 100 and I call the  
function, I expect that calling undo will (undo, obviously, and)  
bring me back to line 100, while it brings me to the beginning of the  
file. This happens also with your (much) shorter version. Doesn't it  
happen to you?

Best regards,
Sandro

On Sep 23, 2008, at 2:05 PM, John Beckett wrote:

> function! TestFunction()
>   " Insert terminator at beginning and end of buffer.
>   0put ='FMT_LTX_TERM'
>   $put ='FMT_LTX_TERM'
>   " ...
>   " Remove terminators.
>   1d
>   $d
> endfunction


--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Strange undo behaviour when calling a function

Charles-14
In reply to this post by Sandro Bosio

On Sep 23, 5:46 pm, Sandro Bosio <[hidden email]> wrote:
> I simply want the undo to jump at the line where the fuction was  
> called. Is this something related to my version (see below) or not?  
> Or it simply should be like that, and there's something I don't get?

I think it's the designed behavior of undo since the change happened
on line 1

To get back on previous position when undo-ing, you can use this
commands

:keepjumps u
'' -> 2x single quotes in normal mode


--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Strange undo behaviour when calling a function

Sandro Bosio


On Sep 23, 2008, at 4:38 PM, char101 wrote:
> I think it's the designed behavior of undo since the change happened
> on line 1

Well, then it is quite strange that if you invert the order of the  
commands, it still goes to the beginning of the file, and not to the  
end of file, isn't it?

And I already tried to make as first command a fake change in the  
position in which I would like the undo to end up. But it does not  
work, it invariably goes to the beginning of the buffer.

> To get back on previous position when undo-ing, you can use this
> commands
>
> :keepjumps u
> '' -> 2x single quotes in normal mode

This workaround more or less works, but I was hoping for a better  
solution (something the user is not required to do, when using the  
function as a black box).

Sandro

> >


--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Strange undo behaviour when calling a function

Tony Mechelynck

On 23/09/08 17:49, Sandro Bosio wrote:

>
> On Sep 23, 2008, at 4:38 PM, char101 wrote:
>> I think it's the designed behavior of undo since the change happened
>> on line 1
>
> Well, then it is quite strange that if you invert the order of the
> commands, it still goes to the beginning of the file, and not to the
> end of file, isn't it?
>
> And I already tried to make as first command a fake change in the
> position in which I would like the undo to end up. But it does not
> work, it invariably goes to the beginning of the buffer.
>
>> To get back on previous position when undo-ing, you can use this
>> commands
>>
>> :keepjumps u
>> '' ->  2x single quotes in normal mode
>
> This workaround more or less works, but I was hoping for a better
> solution (something the user is not required to do, when using the
> function as a black box).
>
> Sandro

Undo always brings you to where the just undone change happened. In your
case (calling a function which always modifies at least the first and
last lines of the file, and maybe other parts), it is a complex change
spanning the whole file. I believe that in this case, bringing you to
line 1 is "normal" undo behaviour. If you want an "undo that doesn't
move the cursor" you can use for instance

        :noremap u m'u``

(small-emm-for-Mike apostrophe small-you-for-Uniform backtick backtick)


Best regards,
Tony.
--
Be prepared... that's the Boy Scout's solemn creed.
Be prepared... to be clean in word and deed.
Don't solicit for your sister, that's not nice,
Unless you get a good percentage of her price ...
                -- Tom Lehrer

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

RE: Strange undo behaviour when calling a function

JohnBeckett
In reply to this post by Sandro Bosio

Sandro Bosio wrote:
> Well, the problem is that if I am, say, on line 100 and I
> call the function, I expect that calling undo will (undo,
> obviously, and) bring me back to line 100, while it brings me
> to the beginning of the file. This happens also with your
> (much) shorter version. Doesn't it happen to you?

Hmmm. I see what you mean. As others have mentioned, I think the problem is that Vim
regards calling a function as a single undo event (that is a feature). There may be
workarounds I'm unaware of, but it looks as if Vim notes the first line modified by
a function (I confirmed that by having the function insert the first terminator at
line 5 rather than the beginning). When you undo, all operations performed by the
function are undone and the cursor goes to the first modified line (line 5 in my
test).

Perhaps you don't need the terminator lines at beginning/end? Look for a way to
format just the para?

John


--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Strange undo behaviour when calling a function

Sandro Bosio

On Sep 24, 2008, at 2:08 AM, John Beckett wrote:

>
> Sandro Bosio wrote:
>> Well, the problem is that if I am, say, on line 100 and I
>> call the function, I expect that calling undo will (undo,
>> obviously, and) bring me back to line 100, while it brings me
>> to the beginning of the file.
>
> Hmmm. I see what you mean. As others have mentioned, I think the  
> problem is that Vim
> regards calling a function as a single undo event (that is a feature).
> There may be workarounds I'm unaware of, but it looks as if Vim  
> notes the first line modified by
> a function.

I thought that, even if it considers it as a global undo event (and I  
agree that it is a feature), the most obvious (and expected)  
behaviour would have been to undo the changes in the reverse order,  
thus ending up at the place of the first change. Or, even better (to  
me at least), to the place where the function was called. But at the  
first modification linewise what sense does it make?

By the way, the fact that calling a function is a single undo event  
has its own limits as well. Apart from user input, which is described  
in the help, If in the middle of the function you change buffer, and  
then you go back to the original buffer, the undo is split in two  
blocks.

> Perhaps you don't need the terminator lines at beginning/end? Look  
> for a way to
> format just the para?

I search for some specific patterns, and if I don't find them it  
prompts an error. Maybe If I (learn to) use try-catch blocks I can  
avoid the terminators. I should definitely buy a book for vim  
scripting, learning by trial end error and google search, and looking  
into other's (frequently uncommented) scripts takes soooo long...

Thanks everybody for your replies!
Sandro

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Strange undo behaviour when calling a function

Andy Wokula
In reply to this post by Sandro Bosio

Sandro Bosio schrieb:
> Hallo,
>
> I have a function whose purpose is to format a paragraph in a latex  
> file. If I undo the call to the function, it does the undo properly,  
> but it jumps to the beginning of the file.

why do you need temporary 'FMT_LTX_TERM' markers for processing?
in the _first_ and _last_ line of the file??

quite sure there will be a solution without these markers.

--
Andy


--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

RE: Strange undo behaviour when calling a function

JohnBeckett
In reply to this post by Sandro Bosio

Sandro Bosio wrote:
> I search for some specific patterns, and if I don't find them
> it prompts an error.

If you call search() it won't give an error (:help search()). If you can give a
simple example (before/after) of what you want to do, there might be some more
suggestions we could give.

John


--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---