Skip to content

Instantly share code, notes, and snippets.

@dmlary
Last active January 6, 2023 14:31
Show Gist options
  • Save dmlary/830597a4e94674fb6b3824260f53d99f to your computer and use it in GitHub Desktop.
Save dmlary/830597a4e94674fb6b3824260f53d99f to your computer and use it in GitHub Desktop.
fzf-vim cscope integration
" Requires the fzf-vim plugin to work. Performs cscope queries against
" cscope.out, uses fzf-vim to display results and previews.
" specify multiple cscope databases to search.
" " short form, list of directories containing cscope.out
" let g:fzf_cscope_databases = [".", "~/kernels/2.6.12"]
"
" " long form database in separate directory from sources
" let g:fzf_cscope_databases = [
" [".", "build/cscope.out"],
" ["/opt/kernels/2.6.12", "~/.cscope/kernel-2.6.12-cscope.out"]]
let g:fzf_cscope_databases = get(g:, "fzf_cscope_databases", ["."])
" type values copied from :cs command
" a: Find assignments to this symbol
" c: Find functions calling this function
" d: Find functions called by this function
" e: Find this egrep pattern
" f: Find this file
" g: Find this definition
" i: Find files #including this file
" s: Find this C symbol
" t: Find this text string
function! Cscope(type, query)
if a:type == "a"
let arg = "-9"
elseif a:type == "c"
let arg = "-3"
elseif a:type == "d"
let arg = "-2"
elseif a:type == "e"
let arg = "-6"
elseif a:type == "f"
let arg = "-7"
elseif a:type == "g"
let arg = "-1"
elseif a:type == "i"
let arg = "-8"
elseif a:type == "s"
let arg = "-0"
elseif a:type == "t"
let arg = "-4"
else
echom "invalid type; use types from :cs command"
return
end
let cmds = []
for cfg in g:fzf_cscope_databases
if type(cfg) != type([])
let cfg = [cfg]
endif
let path = cfg[0]
" skip the path if it's the same as cwd; we assume they have cwd in the
" database list
if getcwd() == path
continue
endif
let reffile = get(cfg, 1, path . "/cscope.out")
let cmd = "cscope -dL -f " . reffile . " " . arg . " '" . a:query . "'"
if path != "."
let cmd = cmd . " | sed 's%^%" . path . "/%'"
end
let cmds += [cmd]
endfor
let cmd = "(" . join(cmds, ";") . ")"
" convert "<file> <func> <line number> <line" into a colorized
" "<file>:<line number>:<func> <line>"
let cmd = cmd . ' | sed "s/^\([^ ]*\) \([^ ]*\) \([0-9]*\)/\1:\3:\2 /"'
" echom cmd
call fzf#vim#grep(cmd, 2, fzf#vim#with_preview(), 0)
endfunction
command! -bang -nargs=* CscopeFindAssignments call Cscope("a", <q-args>)
command! -bang -nargs=* CscopeFindCallers call Cscope("c", <q-args>)
command! -bang -nargs=* CscopeFindCalled call Cscope("d", <q-args>)
command! -bang -nargs=* CscopeFindEgrep call Cscope("e", <q-args>)
command! -bang -nargs=* CscopeFindFile call Cscope("f", <q-args>)
command! -bang -nargs=* CscopeFindGlobal call Cscope("g", <q-args>)
command! -bang -nargs=* CscopeFindInclude call Cscope("i", <q-args>)
command! -bang -nargs=* CscopeFindSymbol call Cscope("s", <q-args>)
command! -bang -nargs=* CscopeFindTextString call Cscope("t", <q-args>)
" handful of frequently used mappings
nnoremap <silent> <Leader>cg :call Cscope("g", "<C-R><C-W>")<CR>
nnoremap <silent> <Leader>cc :call Cscope("c", "<C-R><C-W>")<CR>
nnoremap <silent> <Leader>cf :call Cscope("f", "<C-R><C-W>")<CR>
nnoremap <silent> <Leader>cs :call Cscope("s", "<C-R><C-W>")<CR>
nnoremap <silent> <Leader>ct :call Cscope("t", "<C-R><C-W>")<CR>
nnoremap <Leader>cG :CscopeFindGlobal<SPACE>
nnoremap <Leader>cC :CscopeFindCallers<SPACE>
nnoremap <Leader>cF :CscopeFindFile<SPACE>
nnoremap <Leader>cT :CscopeFindTextString<SPACE>
nnoremap <Leader>cS :CscopeFindSymbol<SPACE>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment