Skip to content

Instantly share code, notes, and snippets.

@weilbith
Last active July 26, 2019 22:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weilbith/bfea9fa4f10e3ce7e93d15723a4b4668 to your computer and use it in GitHub Desktop.
Save weilbith/bfea9fa4f10e3ce7e93d15723a4b4668 to your computer and use it in GitHub Desktop.
Vim - Dynamic Location Lists
" Cache a possibly filled location list for a hiding buffer.
" Check if there is a non-empty list for the current buffer.
" Stores the list to the cache and empty the original one.
" The location window will be closed.
"
" Arguments:
" buffer - buffer to create the cache for
"
function! utils#location#cache_location_list(buffer) abort
if get(g:, 'dynamic_location_list_disable', v:false) | return | endif
let l:location_list = getloclist(0)
" Check if something exist to cache.
if len(l:location_list) > 0
let s:map_buffer_location_list[a:buffer] = l:location_list " Cache the location list for the buffer.
call setloclist(0, []) " Remove since else it remains for the next buffer.
lclose " Close the empty window (will be possibly reopened on restore for new buffer).
endif
endfunction
" Restore a possibly cached location list for a new displayed buffer.
" Check the cache if a location list has been stored for the buffer.
" If so it fills the list with the stored one and opens the window.
" The cache entry will be removed.
"
" Arguments:
" buffer - buffer to restore the cache for
"
function! utils#location#restore_location_list(buffer) abort
if get(g:, 'dynamic_location_list_disable', v:false) | return | endif
" Check cache for the entered buffer.
if has_key(s:map_buffer_location_list, a:buffer)
call setloclist(0, s:map_buffer_location_list[a:buffer]) " Restore the location list from cache.
unlet s:map_buffer_location_list[a:buffer] " Remove cache to avoid reloading.
lopen " Open the filled window.
wincmd p " Jump back from location list to actual window.
endif
endfunction
augroup Location
autocmd!
autocmd BufWinEnter * call utils#location#restore_location_list(expand('<abuf>'))
autocmd BufWinLeave * call utils#location#cache_location_list(expand('<abuf>'))
augroup END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment