stefandtw / quickfix-reflector.vim

Change code right in the quickfix window
MIT License
340 stars 19 forks source link

Prevent quickfix buffer from writing to a file #26

Closed jeromedalbert closed 5 years ago

jeromedalbert commented 5 years ago

I have this handy bit of code that autowrites files when focus is lost:

augroup improved_autowrite
  autocmd!
  autocmd FocusLost,BufLeave * call Autowrite()
augroup end

function! Autowrite()
  silent! wa
endfunction

However, this also writes the quickfix buffer to a file if I have it open and change focus. For now I was able to mitigate with

function! Autowrite()
  if bufname('%') =~ 'quickfix-' | return | endif
  silent! wa
endfunction

But this only works if the current buffer is the quickfix list. If I switch to another buffer, it will still write the file.

Is there some way to prevent this? Maybe somehow prevent the quickfix list to ever be written to a file?

stefandtw commented 5 years ago

Here are two things you could try:

jeromedalbert commented 5 years ago

Thanks for the pointers!

I want to keep using :wa so that autowriting works even when switching tabs. And windo :update will sometimes write to the quicklist buffer when the buffer is modified.

After some poking around, I ended up with the following code:

function! Autowrite()
  for tabnum in range(tabpagenr('$'))
    for bufnum in tabpagebuflist(tabnum + 1)
      if bufname(bufnum) =~ '^quickfix-' | return | endif
    endfor
  endfor
  silent! wa
endfunction

This basically disables autowriting if the quickfix list is open anywhere in Vim. This is good enough behavior for me, as I never leave the quickfix list open for very long. I could further improve by falling back to single-file :w autowriting if needed.