2

Using getwininfo it is possible to determine whether an window is a quickfix window or location list window.

But once a location list window is found, how can I know which window it is associated with?

Is there a more straightforward way to know whether the current window has a location list window opened

4

how to check whether the location list for the current window is open

You could try to inspect the 'winid' property of the location list. If the location window is open, the value of the property should be different than zero:

if get(getloclist(0, {'winid':0}), 'winid', 0)
    " the location window is open
    ...
else
    " the location window is closed
    ...
endif

once a location list window is found, how can I know which window it is associated with?

You could try to inspect the 'filewinid' property of the location list:

echo get(getloclist(0, {'filewinid':0}), 'filewinid', 0)

This requires the Vim patch 8.1.0345.

If your Vim version doesn't include this patch, or if you use Neovim which doesn't implement this property at the moment, you could try this code instead:

fu! Get_associated_window() abort
    let win_ids = get(gettabinfo(tabpagenr())[0], 'windows', {})
    let loc_id = win_getid()
    return get(filter(win_ids, {i,v -> get(getloclist(v, {'winid': 0}), 'winid', 0) ==# loc_id && v !=# loc_id }) , 0, 0)
endfu
echo Get_associated_window()

For more information, see:

:h getloclist()
:h getqflist()
-1

I think that quickfix/loclist windows are not really associated with any other windows/buffers, and that what you ask is not really possible out of the box.

However, if we assume that a quickfix/loclist window is associated with the window that was active when opening, then you could use something like this:

augroup qfidentifier
  autocmd!
  autocmd QuickFixCmdPre [c]* let g:quicklist_buffer = bufnr('.')
  autocmd QuickFixCmdPre [l]* let g:loclist_buffer = bufnr('.')
augroup END

This should set the g:quicklist_buffer and g:loclist_buffer variables to the buffer number that was active when the relevant quickfix or loclist command was executed. I think that should provide the relevant information that you want.

Note: I did not test this, so it might need some adjustments.

  • If I am correct, location list is associated with a window (each window can have a location list, as termed "window-local quickfix list" in :h location-list) – Liu Sha Nov 27 '18 at 8:14
  • Yes, you might be right. And so the answer might not be very good, sorry. Let's hope someone else knows, then. :) – Karl Yngve Lervåg Nov 27 '18 at 9:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.