Skip to content

Instantly share code, notes, and snippets.

@AndrewRadev
Created September 15, 2016 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndrewRadev/cc68443ced6bdf3f684c35709505cf73 to your computer and use it in GitHub Desktop.
Save AndrewRadev/cc68443ced6bdf3f684c35709505cf73 to your computer and use it in GitHub Desktop.
" The filename used for the cfile
let s:cfile_filename = ''
" The last mtime of the filename
let s:cfile_mtime = -1
" Define a command that can be called like:
"
" Cfile errors.txt
"
" Apart from loading the error file in the quickfix window, it also
" auto-updates it every 200ms if it's changed.
"
command! -nargs=1 -complete=file Cfile call s:Cfile(<f-args>)
function! s:Cfile(filename)
let s:cfile_filename = a:filename
" Update every 200ms
let timer = timer_start(200, function('s:UpdateCfile'), {'repeat': -1})
" First "update" to actually load the qf window immediately
call s:UpdateCfile(timer)
endfunction
function! s:UpdateCfile(timer_id)
" Stop the timer if the file is deleted
if s:cfile_filename == '' || !filereadable(s:cfile_filename)
call timer_stop(a:timer_id)
let s:cfile_filename = ''
let s:cfile_mtime = -1
return
endif
" Get file mtime
let mtime = system('stat -c %Y '.shellescape(s:cfile_filename))
" Load the file in the quickfix window if the mtime is newer than the last
" recorded one
if mtime > s:cfile_mtime
exe 'cfile '.s:cfile_filename
let s:cfile_mtime = mtime
endif
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment