for the problem 1, there is solution: You could save the cursor position before you do re-indent (gg=G
), and after that restore the position. vim has build-in getpos() and setpos()
, which could help you in this case. For example, you could create a function:
function! Hook()
let p = getpos(".")
normal! gg=G
call setpos(".",p)
endfunction
and in your autocmd, you call the function:
:autocmd BufLeave,FocusLost * :call Hook()
This should make your vim re-indent and keep the original when you leave and back to a buffer.
However for the problem 2, it seems that there is no good solution with vimscript. Because vimscript doesn't support multi-threading. If the reindent will take 30s, you have to wait when the autocmd was triggered. There could be a solution to implement multi-threads by python/perl. However I haven't tested. Vim 7.4 has improved python interface, you may want to give the multi-threading in python a try.