What is the best way to distinguish the current buffer is location list or quickfix list?

前端 未结 4 1901

I have an autocmd, if ft is qf, it is gonna call some functions to modify the quickfix list by get/setqflist()

I know there ar

相关标签:
4条回答
  • 2021-01-02 05:31

    There is a way to do this. Here's how:

    You get the output of the "ls" command, which shows you the currently active buffers. Then you simply search that output for the existence/state of whatever window you want.

    The example below will tell you if either the Quickfix or Location list window is focused:

    function! example()
        exec 'redir @a | ls | redir END'
        if match(@a,'%a-  "\[Location List\]"') >= 0
                exec 'echo "Location list focused!"'
        elseif match(@a,'%a-  "\[Quickfix List\]"') >= 0
                exec 'echo "Quickfix list focused!"'
        else
                exec 'echo "Neither Location or Quicklist focused!"'
        endif
    endfunction
    

    I appreciate you asking this question and I noted that you even went so far as to try to find help elsewhere: http://vim.1045645.n5.nabble.com/detect-QuickFix-window-list-or-LocationList-td4952180.html.

    I looked around the internet too, and found nothing. I think this answer should solve the problem.

    0 讨论(0)
  • 2021-01-02 05:38

    Just for new comers: as of now, there is getwininfo() which returns dicts containing key quickfix which can be used for the check. Also note the loclist key.

    Since getwininfo returns a list of dict, you may use:

    getwininfo(win_getid())[0]['quickfix']
    

    which is 1 when it is a quickfix or location list window. And

    getwininfo(win_getid())[0]['loclist']
    

    which is 1 only when it is a location list.

    0 讨论(0)
  • 2021-01-02 05:43

    The w:quickfix_title variable tells you what command was used to generate the list displayed in the window. If the first letter after the colon is an l you are in location list.

    0 讨论(0)
  • 2021-01-02 05:46

    The only reliable way I've found, is to store the current window number, then issue a :copen command. If the active window number changes, you were in the location list and not the quickfix list. If it doesn't change, you were already in the quickfix list.

    You also want to store the number of open windows, and compare that to the new number of open windows. Then you can close the quickfix window if it wasn't already open.

    Finally you can jump back to the location list (if needed) using the stored window number and wincmd w.

    0 讨论(0)
提交回复
热议问题