Question

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.

Was it helpful?

Solution

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:

  1. Commit message.
  2. Current commit hex (you can see it using hg log -r .).
  3. 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:

  1. If commit failed I want to restore commit message (same as yours).
  2. If I did hg rollback, edited something and want to commit again I want to restore commit message.
  3. If I closed commit buffer because I remembered I want to code some more changes I want to …
  4. 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.

OTHER TIPS

I don't think that's possible (at least I could not achieve this, even when :throwing an exception on BufUnload), but nothing prevents you from re-:editing the same file (assuming it was properly persisted) immediately again. You can get the filespec from within the autocmd via expand('<afile>').

With commit messages, it usually works out the other way: The revision control system triggers the commit message editor, and aborts the commit when the message is empty or the editor quit with an error (which in Vim you can achieve with :cquit).

To recall such aborted commit messages from a re-invocation of Vim, I've written the VcsMessageRecall plugin. Might help you achieve your goal.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top