2

When we use cscope to go to definition of a symbol in vim, lots of candidates may be shown in result window. I'd like to perform searching within the window to find what I need quickly. But search function (/) doesn't seem to work in result window, only a few keys are available, j,k,gg,G, etc.

Is there anyway to search in cscope result window? Or can anyone share some experiences with how to work more efficiently in such a situation.

Thanks.

Raymond
  • 744
  • 5
  • 12

1 Answers1

2

You can use the following:

" Filter the quickfix list
function! FilterQFList(type, action, pattern)
    " get current quickfix list
    let s:curList = getqflist()
    let s:newList = []
    for item in s:curList
        if a:type == 0     " filter on file names
            let s:cmpPat = bufname(item.bufnr)
        elseif a:type == 1 " filter by line content
            let s:cmpPat = item.text . item.pattern
        endif
        if item.valid
            if a:action < 0
                " Keep only nonmatching lines
                if s:cmpPat !~ a:pattern
                    let s:newList += [item]
                endif
            else
                " Keep only matching lines
                if s:cmpPat =~ a:pattern
                    let s:newList += [item]
                endif
            endif
        endif
    endfor
    call setqflist(s:newList)
endfunction

Then define four mappings (replace ø with something that fits for you, mine start with ð which I think might be unavailable on your keyboard) that map respectively to:

nnoremap ø :call FilterQFList(0, -1, inputdialog('Remove file names matching:', ''))<CR>
nnoremap ø :call FilterQFList(0, 1, inputdialog('Keep only file names matching:', ''))<CR>
nnoremap ø :call FilterQFList(1, -1, inputdialog('Remove all lines matching:', ''))<CR>
nnoremap ø :call FilterQFList(1, 1, inputdialog('Keep only lines matching:', ''))<CR>

This way you can filter your quickfix list with any pattern (you have the power of vim reg.exps). Use :cnewer and :colder to jump through previous quickfix lists.

Benoit
  • 76,634
  • 23
  • 210
  • 236
  • Benoit, thanks for sharing. I can understand your script. But I don't get how to use it together with cscope. The output of cscope doesn't seem to go to quick fix window. Can you show how you make use of it? – Raymond Jan 10 '11 at 14:21
  • 1
    Did you use `:cscope add my_cscope_database.out` then `:cscope find s your_symbol` ? Also make sure that `:set csqf=s-,c-,d-,i-,t-,e-`. Also see `:help cscope-options`. – Benoit Jan 10 '11 at 14:26
  • I didn't notice this option. Thank you so much! – Raymond Jan 10 '11 at 15:09
  • BTW, do you know the "official name" of the default cscope output window? – Raymond Jan 11 '11 at 01:00
  • It is called the quickfix window. See `:help quickfix`! – Benoit Jan 11 '11 at 06:06
  • I know quickfix window. I mean what's the name of the default output window, which is used if I don't instruct cscope to use quickfix window. – Raymond Jan 11 '11 at 06:27
  • Default is the location list window, which is the same as the quickfix window except that it works with commands of approximately the same name but with an `l` in front of the command (`:lnew`, `:lold`, `:ln`, etc.) – Benoit Jan 11 '11 at 06:38