I'm trying to implement a simple autocommand, which deletes a buffer when it's hidden, with a condition that the file hasn't been modified in the last N seconds. I have the following code in
function tools#TimeSinceModified(file) return localtime() - getftime(a:file) endfunction let s:delete_buf_after_s = 1800 function tools#DeleteBuffer(bufnr) if !&modified && tools#TimeSinceModified(bufname(a:bufnr)) > s:delete_buf_after_s execute 'bdelete' a:bufnr endif endfunction
Then, in my
.vimrc, I have this autocommand:
autocmd BufHidden * call tools#DeleteBuffer(expand("<abuf>"))
This doesn't work, however. The buffer isn't deleted, and the editor prompts the following error whenever a buffer is being hidden:
Error detected while processing function tools#DeleteBuffer: line 2: E937: Attempt to delete a buffer that is in use
Is there some way of getting around this error? How can I get this functionality to work?