Use Gtags__vim in VIM

Source: Internet
Author: User


Download Global on the website download global source

Http://www.gnu.org/software/global/download.html


Compile and install Global./configure
Make sudo make install

Gtags vim script configures copy global source directory to Gtags-cscope.vim,gtags.vim to. Vim directory, I use Neobundle management plugin


The. VIMRC is configured as follows,

You can automatically choose to load index database files in Cscope.out and Gtags.

The script will first detect if the Gtags file exists,

If it does not exist, find cscope.out,


"Gtags-cscope setting {{" I use the GNU global instead cscope because global is faster.
"Need install Global
"1. Install Global
"2. Copy/usr/local/share/gtags/gtags-cscope.vim Gtags.vim to
"~/.vim/bundle/gtags.vim/plugin/
"No needed now with the follow function replace the Gtags-cscope.vim plugin
function! S:gtagscscope_gtagsroot ()
Let S:global_command = "global"
Let cmd = S:global_command. "-PQ"
Let Cmd_output = System (CMD)
Return Strpart (cmd_output, 0, strlen (cmd_output)-1)
Endfunction


function! S:gtagscscope ()
"
"Get Gtagsroot directory."
"
Let Gtagsroot = S:gtagscscope_gtagsroot ()
if (!empty (gtagsroot))
"

"Load gtags-cscope."

Set Cscopetag set Csprg=gtags-cscope
Let S:command = "cs Add". Gtagsroot. "/gtags"
Let s:option = '
"If G:gtagscscope_ignore_case = 1
Let s:option = s:option. C
"EndIf
"If G:gtagscscope_absolute_path = 1
Let s:option = s:option. A
"EndIf
"If g:gtagscscope_keep_alive = 1
Let s:option = s:option. I
"EndIf
If S:option!= '
"Let S:command = S:command." -' . S:option
endif
Set Nocscopeverbose
Let S:command = "cs Add". Gtagsroot. "/gtags". ' . -' . ' I '. ' A '. C
Let S:command = "cs Add". Gtagsroot. "/gtags". ' . -' . ' I '. A
EXE S:command
Set Cscopeverbose
"
"Key Mapping
"
"If G:gtagscscope_auto_map = 1
"The following key mappings are derived from ' Cscope_maps.vim '.
"(The ' d ' command isn't implemented in Gtags-cscope.)
"
The normal command
: Nmap <c-\>s:cs Find S <c-r>=expand ("<cword>") <CR><CR>
: Nmap <c-\>g:cs Find G <c-r>=expand ("<cword>") <CR><CR>
: Nmap <c-\>c:cs Find C <c-r>=expand ("<cword>") <CR><CR>
: Nmap <c-\>t:cs Find T <c-r>=expand ("<cword>") <CR><CR>
: Nmap <c-\>e:cs Find e <c-r>=expand ("<cword>") <CR><CR>
: Nmap <c-\>f:cs Find F <c-r>=expand ("<cfile>") <CR><CR>
: Nmap <c-\>i:cs Find I <c-r>=expand ("<cfile>") <CR><CR>
": Nmap <c-\>d:cs Find D <c-r>=expand (" <cword> ") <CR><CR>
"Using ' Ctrl-spacebar ', the result is displayed in new horizontal window.
"EndIf
endif
Endfunction


function! Updategtags (f)
Let dir = fnamemodify (A:f, ':p: H ')
EXE ' Silent!CD '. Dir. ' && global-u &>/dev/null & '
Endfunction


"==
"Windowdir
"Gets the directory for the"
"Or the current working dir-if there isn ' t one for the" window.
"Use TR-Allow" other OS paths, too
function! S:windowdir ()
If WINBUFNR (0) = = 1
Let Unislash = GETCWD ()
Else
Let Unislash = Fnamemodify (bufname (WINBUFNR (0)), ':p: H ')
endif
return tr (unislash, ' \ ', '/')
Endfunc
"
"==
"Find_in_parent
"Find the" file argument and returns the path to it.
"Starting with the" working dir, it walks up the parent folders
"Until it finds the" file, or it hits the Stop dir.
"If it doesn ' t find it, it returns" Nothing "
function! S:find_in_parent (FLN,FLSRT,FLSTP)
Let's here = A:flsrt
while (strlen) > 0)
If Filereadable (here). "/" . A:FLN)
Return to Here
endif
Let FR = Match (here, "/[^/]*$")
If FR = = 1
Break
endif
Let's = Strpart (here, 0, FR)
if here = = A:FLSTP
Break
endif
Endwhile
Return ' Nothing '
Endfunc


"The following function replaces the Autoload_cscope.vim
"==
"Unload_csdb
"Drop cscope connections.
function! S:unload_csdb ()
If exists ("B:csdbpath")
If Cscope_connection (3, "out", B:csdbpath)
Let SAVE_CSVB = &csverb
Set Nocsverb
EXE "CS kill". B:csdbpath
Set Csverb
Let &csverb = Save_csvb
endif
endif
Endfunc
"
"==
"Cycle_csdb
"Cycle the loaded Cscope db.
function! S:cycle_csdb ()
If exists ("B:csdbpath")
If Cscope_connection (3, "out", B:csdbpath)
Return
"It is already loaded." Don ' t try to reload it.
endif
endif
Let Newcsdbpath = S:find_in_parent ("Cscope.out", S:windowdir (), $HOME)
"Echo" Found cscope.out at: ". Newcsdbpath
"Echo" Windowdir: ". S:windowdir ()
If Newcsdbpath!= "Nothing"
Let B:csdbpath = Newcsdbpath
If!cscope_connection (3, "out", B:csdbpath)
Let SAVE_CSVB = &csverb
"Set Nocsverb
Set Csverb
EXE "CS add". B:csdbpath. "/cscope.out". B:csdbpath
Let &csverb = Save_csvb
endif
"
Else "No Cscope database, undo things." (Someone rm-ed it or somesuch)
Call S:unload_csdb ()
endif
Endfunc


"If gtags exists, use Gtags-cscope
"If Gtags does not exist, use Cscope
function! Selectcscopedb ()
Set nocscopeverbose "Suppress ' duplicate connection ' error
Let Gtagsroot = S:gtagscscope_gtagsroot ()
if (!empty (gtagsroot))
Set Cscopeprg=gtags-cscope
Set Cscopetag
Let S:command = "cs Add". Gtagsroot. "/gtags". ' . -' . ' I '. A
EXE S:command


"Automatically update gtags when saving files
"Au bufwritepost *. [CH] Call Updategtags (expand (' <afile> '))
"Au bufwritepost *. [Ch]pp Call Updategtags (expand (' <afile> '))
"Au bufwritepost *. [Ch]xx Call Updategtags (expand (' <afile> '))
"Au bufwritepost *.java call updategtags" (Expand (' <afile> '))
"Au bufwritepost *.cc call updategtags" (Expand (' <afile> '))
Else
Set Csprg=/usr/bin/cscope
"Use both Cscope and Ctag
Set Cscopetag
"Use Cscope for definition search"
Set cscopetagorder=0
Let G:autocscope_menus=0
Set cscopetagorder=0
The Auto Toggle the menu
Augroup Autoload_cscope
au!
Au bufenter *. [chly] Call <sid>cycle_csdb ()
Au bufenter *.cc call <sid>cycle_csdb ()
Au bufenter *.cpp call <sid>cycle_csdb ()
Au bufenter *.java call <sid>cycle_csdb ()
Au bufunload *. [chly] Call <sid>unload_csdb ()
Au bufunload *.cc call <sid>unload_csdb ()
Au bufunload *.cpp call <sid>unload_csdb ()
Au bufunload *.java call <sid>unload_csdb ()
Augroup End
Nmap <c-\>d:cs Find D <c-r>=expand ("<cword>") <CR><CR>
endif
"Set cscopequickfix=c-,d-,e-,f-,g0,i-,s-,t-
"" To resolve cscope and tag when they coexist Ctrl +] Sometimes an abnormal bug
"Nmap <C-]>: TJ <c-r>=expand (" <cword> ") <CR><CR>
Nmap <c-\>s:cs Find S <c-r>=expand ("<cword>") <CR><CR>
Nmap <c-\>g:cs Find G <c-r>=expand ("<cword>") <CR><CR>
Nmap <c-\>c:cs Find C <c-r>=expand ("<cword>") <CR><CR>
Nmap <c-\>t:cs Find T <c-r>=expand ("<cword>") <CR><CR>
Nmap <c-\>e:cs Find e <c-r>=expand ("<cword>") <CR><CR>
Nmap <c-\>f:cs find F <c-r>=expand ("<cfile>") <CR><CR>
Nmap <c-\>i:cs Find I <c-r>=expand ("<cfile>") <CR><CR>
Set Cscopeverbose
Endfunc


Autocmd FileType C,cpp,java,python Call Selectcscopedb ()
command! -nargs=0 Gtagscscope call S:gtagscscope ()
command! -nargs=0 Cscope call S:cycle_csdb ()
"}}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.