Monday, July 19, 2010

Re: function that doesn't modify undo list

Hi bartek!

On Mo, 12 Jul 2010, bartek wrote:

> Hi all,
> I have written a vim function that look like this:
> (PSEUDO-vim-code)
>
> function Sth()
> org = getline('.')
> new = org
> new[5] = '+'
> new[9] = '*'
>
> setline('.', new)
> matchadd(...) " highlight current line; 6th and 10th char
> sleep 5 " some other commands
>
> setline('.', org)
> matchdelete(...)
> endfunction
>
> The function temporarily changes buffer, but in the end all changes
> are reverted.
> How can I modify the function so that any changes made in it would not
> be reflected in undo list?

You can't. With ou could clear the undo list manually by reloading the
file (this works up to Vim 7.2, but may not work with vim 7.3 when
persisting undo is enabled) or by setting the undolevels option to 0 and
changing something and undoing that change.

>
> btw:
> The function has one more flaw: it doesn't work well when buffer is
> not modifiable.
> Do you have any suggestions how to fix that?

Don't run the function, if the buffer isn't modifiable (:h 'ma')? Or use
try catch blocks. It's hard to tell, we don't know, why you need to
change the line at all. May be chaging the line isn't necessary at all?

regards,
Christian

--
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

No comments: