Wednesday, November 24, 2010

Undo problem with BufWritePre, BufWritePost

I'm trying to modify the file in BufWritePre when written to disk,
and then undo those modifications in BufWritePost so
text in working buffer remains unchanged. The problem is that
the undo operation always seems to undo the last undo operation
before what was done in BufWritePre, along with the changes
done in BufWritePre.

Here is the code:

" BufWritePre function
function! <SID>GlobalUnconvertTags()
let g:save_cursor = getpos(".")
g/^\*\+\s/call UnconvertTags(line("."))
endfunction
" BufWritePost function
function! <SID>UndoUnconvertTags()
undo
call setpos(".",g:save_cursor)
endfunction

Is something different from a simple undo required? Given these two
functions as they are, if I continually issue the command ':w' the
undostate of my buffer will go incrementally backward, one undo
each time I issue the w command.

I've tried altering the functions to explicitly referance an undo
state (as below) but this isn't working either, has same behavior:

" BufWritePre function
function! <SID>GlobalUnconvertTags()
let g:save_cursor = getpos(".")
let s:undostate = changenr()
g/^\*\+\s/call UnconvertTags(line("."))
endfunction
" BufWritePost function
function! <SID>UndoUnconvertTags()
execute "undo ".s:undostate
call setpos(".",g:save_cursor)
endfunction


I assume I'm missing something simple about how undo is supposed to
work
between these two events, but I can't figure out what. Any hints
appreciated.

Regards,

Herb Sitz


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