Q:

Prevent buffer from unloading in autocmd

I'm writing a BufUnload autocmd, and I want to conditionally prevent the buffer from actually being unloaded or closed from within the autocmd. Is there a way to do this?
For instance, I want to use the contents of the buffer as a commit message for mercurial using an autocmd that does:
autocmd BufUnload <buffer> !hg ci -l logfile

So if the commit fails (the hg command returns non-zero error code), I want to leave the buffer open and unchanged.

A:

I would suggest saving this message in a variable. In aurum (plugin for VCS↔Vim integration) there is the following logic coded: when buffer with commit message is wiped out or written (which triggers actual committing and wiping it out) I save three variables:

Commit message.
Current commit hex (you can see it using hg log -r .).
Current repository root.

. Then when committing something again the following logic is used: if current repository root is the same as the saved one and so is current commit hex then commit message is taken from the variable and initializes the buffer.
Reasons for the following behavior:

If commit failed I want to restore commit message (same as yours).
If I did hg rollback, edited something and want to commit again I want to restore commit message.
If I closed commit buffer because I remembered I want to code some more changes I want to …
If I did not do anything of the above I do not want to see old commit message.

Saving current commit hex is here for distinguishing between first three and the last case.

