1

I run Glog! from fugitive to populate the quickfix window and set the following autocmd for QuickFixCmdPost (The following questions general and are not specific to Glog, I just use it as example)

autocmd QuickFixCmdPost * call func()

function func()
    echomsg 'quickfix: ' . w:quickfix_title
    botright cwindow
    if w:quickfix_title =~ 'git.*log'
        nnoremap <buffer> v <c-w><cr><C-w>L
    endif
endfunction

According to doc (:h w:quickfix_title) w:quickfix_title is supposed to contain the command used, which is something like :git --git-dir=.git --no-pager log --no-color set by fugitive.

However, after running Glog, the func() is called and w:quickfix_title is always empty. But the quickfix window's statusline still displays the title correctly.

What is the problem here? It seems that w:quickfix_title is not set before the autocmd finishes. Calling :cwindow to open the window does not get it set immediately.

A few more questions:

  • Accoring to :h QuickFixCmdPre, the pattern is matched against the command being run, such as :grep, :lgrep, etc. If I run :grep abcdefg, is that abcdefg part of the command and can be used as pattern? If yes, I know that Glog contains git.*log in its command, can I use this as the pattern instead of putting the if condition in func, i.e.

    autocmd QuickFixCmdPost git*log call func()

  • If before running Glog the quickfix window is not open, the following autocmds are not triggered (even if I call cwindow explicitly in func):

    • BufEnter
    • WinEnter
    • BufWinEnter

    However, if the quickfix window is already open, BufWinEnter is trigged. What is the reason for this?

  • What is the correct way to set up some maps in quickfix window after Glog is run
2

w:quickfix_title is a window-local variable (w: part). This means w:quickfix_title will only available after the quickfix window is opened and is focused. This means echomsg w:quickfix_title will not show the quickfix title reliably.

However your first method should work. I checked it and it worked for me after adjusting your function name. You can check for your mapping via nnmap v while you are in the quickfix window.

This sort of works however once the mapping is created then it is not properly cleared if the quickfix list changes. It would be better to always have a quickfix window mapping and have a conditional on the mapping itself.

augroup My_qf
  autocmd!
  autocmd FileType qf nnoremap <expr> <buffer> v getqflist({'title':1}).title =~ 'git.*log' ? \"<c-w>\<cr>\<c-w>L" : "v"
augroup END

You may decide every quickfix window should have this mapping for consistency then the conditional would not be needed so you would end up with:

augroup My_qf
  autocmd!
  autocmd FileType qf nnoremap  <buffer> v <c-w>\<cr>\<c-w>L
augroup END

For more help see:

:h QuickFixCmdPost
:h CTRL-W_<Enter>
:h getqflist()
:h w:
:h w:quickfix_title
:h :map-expression
  • 1
    as you said: "w:quickfix_title is a window-local variable (w: part). This means w:quickfix_title will only available after the quickfix window is opened and is focused". But In func(), cwindow is explicitly called, Isn't that enough to have w:quickfix_title set? – Liu Sha Nov 2 '17 at 2:34
  • for your suggestion of using autocmd BufWinEnter quickfix if (w:quickfix_title =~ 'git.*log') | nnoremap <buffer> v <c-w><cr><C-w>L | endif, I already said that BufWinEnter is not triggered by Glog if the quickfix window is not open before running Glog – Liu Sha Nov 2 '17 at 2:39
  • @LiuSha, You are correct, w:quickfix_title should be set (I have updated my post). You are also correct BufWinEnter is triggered "after a buffer is displayed in a window", so this technique should be avoided and I have removed it from the post. You maybe able to WinEnter instead, but I have not tested it. – Peter Rincker Nov 2 '17 at 14:32
0

I finally got this solution: that is to check the &grepprg, which is a string. For Glog, it should match ^git\>.*\<grep\>. (has exceptions since grep may appear as filename)

The function will look like

function func()
    if &grepprg =~ 'git.*log'
        nnoremap <buffer> v <c-w><cr><C-w>L
    endif
endfunction

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.