2

I have the following autocmds to automatically open the quickfix window or the location list window after executing a quickfix command.

augroup quickfix_window
    autocmd!
    autocmd QuickFixCmdPost [^l]* nested cwindow | setlocal nowrap | redraw!
    autocmd QuickFixCmdPost l* nested lwindow | setlocal nowrap | redraw!
augroup END

It seems to work except with the :lhelpgrep command.

If I type :lvim /autocmd/ % to look for all the words matching autocmd inside the current buffer, the location list is populated with all the matches and the location list window is automatically opened (provided there are matches).

But If I type :lh autocmd to look for the word autocmd inside the help files, even though the command is run and the location list is populated with results, the location list window is not opened, I have to do it manually with :lwindow.

According to :h QuickFixCmdPre, The pattern is matched against the command being run..

Here the command name is :lh which begins with the l character, so it should match the pattern l* in the 2nd autocmd.

If I replace :lwindow with :lopen, the previous command :lh autocmd raises the following error:

Error detected while processing QuickFixCmdPost Auto commands for "l*":
E776: No location list

It seems :lh populates the location list after the QuickFixCmdPost event, instead of before like I expected. Is it normal? Is there another way to make the location list window automatically opened after :lh has been run?

  • 2
    Your problem is elsewhere: lhelpgrep opens a help window, and the loclist is associated to the help window, not the window you're starting from. – Sato Katsura Jun 19 '16 at 17:27
  • @SatoKatsura You're right, thank you very much. – user9433424 Jun 19 '16 at 18:29
1

Thanks to Sato Katsura, I've realised that I was wrong, :lh does not populate the location list after the QuickFixCmdPost event, but before like it should.

The problem is that :lh foobar (contrary to :lvim /foobar/ %) creates a help window (if we aren't already in one) to display the first match of our search.
The location list populated by :lh is associated to this newly created window not to the one where we initially typed :lh foobar.

This causes a problem for an autocmd. Because, when the QuickFixCmdPost event is triggered, the help window hasn't been created by :lh yet. So, :lwindow will fail, because there's no location list associated to the current window. A possible solution would be to delay the execution of :lwindow (10ms should be enough).

There may a better way of doing it, but for the time being, I've come up with this:

augroup quickfix_window
    autocmd!
    autocmd QuickFixCmdPost [^l]* nested cwindow | setlocal nowrap | redraw!
    autocmd QuickFixCmdPost l[^h]* nested lwindow | setlocal nowrap | redraw!
    autocmd QuickFixCmdPost lhelpgrep nested call timer_start(10, s:snr() . 'lh_lwindow')
augroup END

fu! s:lh_lwindow(id) abort
    lwindow
    setlocal nowrap
    redraw!
endfu

fu! s:snr()
    return matchstr(expand('<sfile>'), '<SNR>\d\+_')
endfu

It delays the execution of the :lwindow command for 10ms when the quickfix command is :lhelpgrep, which should give enough time for the help window to be created.

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.