My dotfiles. Period.

gtags_cscope.vim 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. " gtags_cscope module for Gutentags
  2. if !has('cscope')
  3. throw "Can't enable the gtags-cscope module for Gutentags, "
  4. \"this Vim has no support for cscope files."
  5. endif
  6. " Global Options {{{
  7. if !exists('g:gutentags_gtags_executable')
  8. let g:gutentags_gtags_executable = 'gtags'
  9. endif
  10. if !exists('g:gutentags_gtags_dbpath')
  11. let g:gutentags_gtags_dbpath = ''
  12. endif
  13. if !exists('g:gutentags_gtags_options_file')
  14. let g:gutentags_gtags_options_file = '.gutgtags'
  15. endif
  16. if !exists('g:gutentags_gtags_cscope_executable')
  17. let g:gutentags_gtags_cscope_executable = 'gtags-cscope'
  18. endif
  19. if !exists('g:gutentags_auto_add_gtags_cscope')
  20. let g:gutentags_auto_add_gtags_cscope = 1
  21. endif
  22. " }}}
  23. " Gutentags Module Interface {{{
  24. let s:added_db_files = {}
  25. function! s:add_db(db_file) abort
  26. if filereadable(a:db_file)
  27. call gutentags#trace(
  28. \"Adding cscope DB file: " . a:db_file)
  29. set nocscopeverbose
  30. execute 'cs add ' . fnameescape(a:db_file)
  31. set cscopeverbose
  32. let s:added_db_files[a:db_file] = 1
  33. else
  34. call gutentags#trace(
  35. \"Not adding cscope DB file because it doesn't " .
  36. \"exist yet: " . a:db_file)
  37. endif
  38. endfunction
  39. function! gutentags#gtags_cscope#init(project_root) abort
  40. let l:db_path = gutentags#get_cachefile(
  41. \a:project_root, g:gutentags_gtags_dbpath)
  42. let l:db_path = gutentags#stripslash(l:db_path)
  43. let l:db_file = l:db_path . '/GTAGS'
  44. let l:db_file = gutentags#normalizepath(l:db_file)
  45. if !isdirectory(l:db_path)
  46. call mkdir(l:db_path, 'p')
  47. endif
  48. let b:gutentags_files['gtags_cscope'] = l:db_file
  49. execute 'set cscopeprg=' . fnameescape(g:gutentags_gtags_cscope_executable)
  50. " The combination of gtags-cscope, vim's cscope and global files is
  51. " a bit flaky. Environment variables are safer than vim passing
  52. " paths around and interpreting input correctly.
  53. let $GTAGSDBPATH = l:db_path
  54. let $GTAGSROOT = a:project_root
  55. if g:gutentags_auto_add_gtags_cscope &&
  56. \!has_key(s:added_db_files, l:db_file)
  57. let s:added_db_files[l:db_file] = 0
  58. call s:add_db(l:db_file)
  59. endif
  60. endfunction
  61. function! gutentags#gtags_cscope#generate(proj_dir, tags_file, gen_opts) abort
  62. " gtags doesn't honour GTAGSDBPATH and GTAGSROOT, so PWD and dbpath
  63. " have to be set
  64. let l:db_path = fnamemodify(a:tags_file, ':p:h')
  65. let l:proj_options_file = a:proj_dir . '/' . g:gutentags_gtags_options_file
  66. let l:cmd = ['"'.g:gutentags_gtags_executable.'"']
  67. if filereadable(l:proj_options_file)
  68. let l:proj_options = readfile(l:proj_options_file)
  69. let l:cmd += l:proj_options
  70. endif
  71. let l:cmd += ['--incremental', '"'.l:db_path.'"']
  72. let l:cmd = gutentags#make_args(l:cmd)
  73. call gutentags#trace("Running: " . string(l:cmd))
  74. call gutentags#trace("In: " . getcwd())
  75. if !g:gutentags_fake
  76. let l:job_opts = gutentags#build_default_job_options('gtags_cscope')
  77. let l:job = gutentags#start_job(l:cmd, l:job_opts)
  78. call gutentags#add_job('gtags_cscope', a:tags_file, l:job)
  79. else
  80. call gutentags#trace("(fake... not actually running)")
  81. endif
  82. call gutentags#trace("")
  83. endfunction
  84. function! gutentags#gtags_cscope#on_job_exit(job, exit_val) abort
  85. let l:job_idx = gutentags#find_job_index_by_data('gtags_cscope', a:job)
  86. let l:dbfile_path = gutentags#get_job_tags_file('gtags_cscope', l:job_idx)
  87. call gutentags#remove_job('gtags_cscope', l:job_idx)
  88. if g:gutentags_auto_add_gtags_cscope
  89. call s:add_db(l:dbfile_path)
  90. endif
  91. if a:exit_val != 0
  92. call gutentags#warning(
  93. \"gtags-cscope job failed, returned: ".
  94. \string(a:exit_val))
  95. endif
  96. endfunction
  97. " }}}