if_cscop.txt 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. *if_cscop.txt* For Vim version 9.0. Last change: 2022 Jan 08
  2. VIM REFERENCE MANUAL by Andy Kahn
  3. *cscope* *Cscope*
  4. This document explains how to use Vim's cscope interface.
  5. Cscope is a tool like ctags, but think of it as ctags on steroids since it
  6. does a lot more than what ctags provides. In Vim, jumping to a result from
  7. a cscope query is just like jumping to any tag; it is saved on the tag stack
  8. so that with the right keyboard mappings, you can jump back and forth between
  9. functions as you normally would with |tags|.
  10. 1. Cscope introduction |cscope-intro|
  11. 2. Cscope related commands |cscope-commands|
  12. 3. Cscope options |cscope-options|
  13. 4. How to use cscope in Vim |cscope-howtouse|
  14. 5. Limitations |cscope-limitations|
  15. 6. Suggested usage |cscope-suggestions|
  16. 7. Availability & Information |cscope-info|
  17. This is currently for Unix and Win32 only.
  18. ==============================================================================
  19. 1. Cscope introduction *cscope-intro*
  20. The following text is taken from a version of the cscope man page:
  21. -----
  22. Cscope is an interactive screen-oriented tool that helps you:
  23. Learn how a C program works without endless flipping through a thick
  24. listing.
  25. Locate the section of code to change to fix a bug without having to
  26. learn the entire program.
  27. Examine the effect of a proposed change such as adding a value to an
  28. enum variable.
  29. Verify that a change has been made in all source files such as adding
  30. an argument to an existing function.
  31. Rename a global variable in all source files.
  32. Change a constant to a preprocessor symbol in selected lines of files.
  33. It is designed to answer questions like:
  34. Where is this symbol used?
  35. Where is it defined?
  36. Where did this variable get its value?
  37. What is this global symbol's definition?
  38. Where is this function in the source files?
  39. What functions call this function?
  40. What functions are called by this function?
  41. Where does the message "out of space" come from?
  42. Where is this source file in the directory structure?
  43. What files include this header file?
  44. Cscope answers these questions from a symbol database that it builds the
  45. first time it is used on the source files. On a subsequent call, cscope
  46. rebuilds the database only if a source file has changed or the list of
  47. source files is different. When the database is rebuilt the data for the
  48. unchanged files is copied from the old database, which makes rebuilding
  49. much faster than the initial build.
  50. -----
  51. When cscope is normally invoked, you will get a full-screen selection
  52. screen allowing you to make a query for one of the above questions.
  53. However, once a match is found to your query and you have entered your
  54. text editor to edit the source file containing match, you cannot simply
  55. jump from tag to tag as you normally would with vi's Ctrl-] or :tag
  56. command.
  57. Vim's cscope interface is done by invoking cscope with its line-oriented
  58. interface, and then parsing the output returned from a query. The end
  59. result is that cscope query results become just like regular tags, so
  60. you can jump to them just like you do with normal tags (Ctrl-] or :tag)
  61. and then go back by popping off the tagstack with Ctrl-T. (Please note
  62. however, that you don't actually jump to a cscope tag simply by doing
  63. Ctrl-] or :tag without remapping these commands or setting an option.
  64. See the remaining sections on how the cscope interface works and for
  65. suggested use.)
  66. ==============================================================================
  67. 2. Cscope related commands *cscope-commands*
  68. *:cscope* *:cs* *:scs* *:scscope* *E259* *E262* *E560* *E561*
  69. All cscope commands are accessed through suboptions to the cscope commands.
  70. `:cscope` or `:cs` is the main command
  71. `:scscope` or `:scs` does the same and splits the window
  72. `:lcscope` or `:lcs` uses the location list, see |:lcscope|
  73. The available subcommands are:
  74. *E563* *E564* *E566* *E568* *E622* *E623* *E625*
  75. *E626* *E609*
  76. add : Add a new cscope database/connection.
  77. USAGE :cs add {file|dir} [pre-path] [flags]
  78. [pre-path] is the pathname used with the -P command to cscope.
  79. [flags] are any additional flags you want to pass to cscope.
  80. EXAMPLES >
  81. :cscope add /usr/local/cdb/cscope.out
  82. :cscope add /projects/vim/cscope.out /usr/local/vim
  83. :cscope add cscope.out /usr/local/vim -C
  84. <
  85. *cscope-find* *cs-find* *E567*
  86. find : Query cscope. All cscope query options are available
  87. except option #5 ("Change this grep pattern").
  88. USAGE :cs find {querytype} {name}
  89. {querytype} corresponds to the actual cscope line
  90. interface numbers as well as default nvi commands:
  91. 0 or s: Find this C symbol
  92. 1 or g: Find this definition
  93. 2 or d: Find functions called by this function
  94. 3 or c: Find functions calling this function
  95. 4 or t: Find this text string
  96. 6 or e: Find this egrep pattern
  97. 7 or f: Find this file
  98. 8 or i: Find files #including this file
  99. 9 or a: Find places where this symbol is assigned a value
  100. For all types, except 4 and 6, leading white space for {name} is
  101. removed. For 4 and 6 there is exactly one space between {querytype}
  102. and {name}. Further white space is included in {name}.
  103. EXAMPLES >
  104. :cscope find c vim_free
  105. :cscope find 3 vim_free
  106. <
  107. These two examples perform the same query: functions calling
  108. "vim_free". >
  109. :cscope find t initOnce
  110. :cscope find t initOnce
  111. <
  112. The first one searches for the text "initOnce", the second one for
  113. " initOnce". >
  114. :cscope find 0 DEFAULT_TERM
  115. <
  116. Executing this example on the source code for Vim 5.1 produces the
  117. following output:
  118. Cscope tag: DEFAULT_TERM
  119. # line filename / context / line
  120. 1 1009 vim-5.1-gtk/src/term.c <<GLOBAL>>
  121. #define DEFAULT_TERM (char_u *)"amiga"
  122. 2 1013 vim-5.1-gtk/src/term.c <<GLOBAL>>
  123. #define DEFAULT_TERM (char_u *)"win32"
  124. 3 1017 vim-5.1-gtk/src/term.c <<GLOBAL>>
  125. #define DEFAULT_TERM (char_u *)"pcterm"
  126. 4 1021 vim-5.1-gtk/src/term.c <<GLOBAL>>
  127. #define DEFAULT_TERM (char_u *)"ansi"
  128. 5 1025 vim-5.1-gtk/src/term.c <<GLOBAL>>
  129. #define DEFAULT_TERM (char_u *)"vt52"
  130. 6 1029 vim-5.1-gtk/src/term.c <<GLOBAL>>
  131. #define DEFAULT_TERM (char_u *)"os2ansi"
  132. 7 1033 vim-5.1-gtk/src/term.c <<GLOBAL>>
  133. #define DEFAULT_TERM (char_u *)"ansi"
  134. 8 1037 vim-5.1-gtk/src/term.c <<GLOBAL>>
  135. # undef DEFAULT_TERM
  136. 9 1038 vim-5.1-gtk/src/term.c <<GLOBAL>>
  137. #define DEFAULT_TERM (char_u *)"beos-ansi"
  138. 10 1042 vim-5.1-gtk/src/term.c <<GLOBAL>>
  139. #define DEFAULT_TERM (char_u *)"mac-ansi"
  140. 11 1335 vim-5.1-gtk/src/term.c <<set_termname>>
  141. term = DEFAULT_TERM;
  142. 12 1459 vim-5.1-gtk/src/term.c <<set_termname>>
  143. if (STRCMP(term, DEFAULT_TERM))
  144. 13 1826 vim-5.1-gtk/src/term.c <<termcapinit>>
  145. term = DEFAULT_TERM;
  146. 14 1833 vim-5.1-gtk/src/term.c <<termcapinit>>
  147. term = DEFAULT_TERM;
  148. 15 3635 vim-5.1-gtk/src/term.c <<update_tcap>>
  149. p = find_builtin_term(DEFAULT_TERM);
  150. Enter nr of choice (<CR> to abort):
  151. The output shows several pieces of information:
  152. 1. The tag number (there are 15 in this example).
  153. 2. The line number where the tag occurs.
  154. 3. The filename where the tag occurs.
  155. 4. The context of the tag (e.g., global, or the function name).
  156. 5. The line from the file itself.
  157. help : Show a brief synopsis.
  158. USAGE :cs help
  159. *E261*
  160. kill : Kill a cscope connection (or kill all cscope connections).
  161. USAGE :cs kill {num|partial_name}
  162. To kill a cscope connection, the connection number or a partial
  163. name must be specified. The partial name is simply any part of
  164. the pathname of the cscope database. Kill a cscope connection
  165. using the partial name with caution!
  166. If the specified connection number is -1, then _ALL_ cscope
  167. connections will be killed.
  168. reset : Reinit all cscope connections.
  169. USAGE :cs reset
  170. show : Show cscope connections.
  171. USAGE :cs show
  172. *:lcscope* *:lcs*
  173. This command is same as the ":cscope" command, except when the
  174. 'cscopequickfix' option is set, the location list for the current window is
  175. used instead of the quickfix list to show the cscope results.
  176. *:cstag* *E257* *E562*
  177. If you use cscope as well as ctags, |:cstag| allows you to search one or
  178. the other before making a jump. For example, you can choose to first
  179. search your cscope database(s) for a match, and if one is not found, then
  180. your tags file(s) will be searched. The order in which this happens
  181. is determined by the value of |csto|. See |cscope-options| for more
  182. details.
  183. |:cstag| performs the equivalent of ":cs find g" on the identifier when
  184. searching through the cscope database(s).
  185. |:cstag| performs the equivalent of |:tjump| on the identifier when searching
  186. through your tags file(s).
  187. ==============================================================================
  188. 3. Cscope options *cscope-options*
  189. Use the |:set| command to set all cscope options. Ideally, you would do
  190. this in one of your startup files (e.g., .vimrc). Some cscope related
  191. variables are only valid within |.vimrc|. Setting them after vim has
  192. started will have no effect!
  193. *cscopeprg* *csprg*
  194. 'cscopeprg' specifies the command to execute cscope. The default is
  195. "cscope". For example: >
  196. :set csprg=/usr/local/bin/cscope
  197. <
  198. *cscopequickfix* *csqf* *E469*
  199. {not available when compiled without the |+quickfix| feature}
  200. 'cscopequickfix' specifies whether to use quickfix window to show cscope
  201. results. This is a list of comma-separated values. Each item consists of
  202. |cscope-find| command (s, g, d, c, t, e, f, i or a) and flag (+, - or 0).
  203. '+' indicates that results must be appended to quickfix window,
  204. '-' implies previous results clearance, '0' or command absence - don't use
  205. quickfix. Search is performed from start until first command occurrence.
  206. The default value is "" (don't use quickfix anyway). The following value
  207. seems to be useful: >
  208. :set cscopequickfix=s-,c-,d-,i-,t-,e-,a-
  209. <
  210. *cscopetag* *cst*
  211. If 'cscopetag' is set, the commands ":tag" and CTRL-] as well as "vim -t"
  212. will always use |:cstag| instead of the default :tag behavior. Effectively,
  213. by setting 'cst', you will always search your cscope databases as well as
  214. your tag files. The default is off. Examples: >
  215. :set cst
  216. :set nocst
  217. <
  218. *cscoperelative* *csre*
  219. If 'cscoperelative' is set, then in absence of a prefix given to cscope
  220. (prefix is the argument of -P option of cscope), basename of cscope.out
  221. location (usually the project root directory) will be used as the prefix
  222. to construct an absolute path. The default is off. Note: This option is
  223. only effective when cscope (cscopeprg) is initialized without a prefix
  224. path (-P). Examples: >
  225. :set csre
  226. :set nocsre
  227. <
  228. *cscopetagorder* *csto*
  229. The value of 'csto' determines the order in which |:cstag| performs a search.
  230. If 'csto' is set to zero, cscope database(s) are searched first, followed
  231. by tag file(s) if cscope did not return any matches. If 'csto' is set to
  232. one, tag file(s) are searched before cscope database(s). The default is zero.
  233. Examples: >
  234. :set csto=0
  235. :set csto=1
  236. <
  237. *cscopeverbose* *csverb*
  238. If 'cscopeverbose' is not set (the default), messages will not be printed
  239. indicating success or failure when adding a cscope database. Ideally, you
  240. should reset this option in your |.vimrc| before adding any cscope databases,
  241. and after adding them, set it. From then on, when you add more databases
  242. within Vim, you will get a (hopefully) useful message should the database fail
  243. to be added. Examples: >
  244. :set csverb
  245. :set nocsverb
  246. <
  247. *cscopepathcomp* *cspc*
  248. The value of 'cspc' determines how many components of a file's path to
  249. display. With the default value of zero the entire path will be displayed.
  250. The value one will display only the filename with no path. Other values
  251. display that many components. For example: >
  252. :set cspc=3
  253. will display the last 3 components of the file's path, including the file
  254. name itself.
  255. ==============================================================================
  256. 4. How to use cscope in Vim *cscope-howtouse*
  257. The first thing you need to do is to build a cscope database for your
  258. source files. For the most basic case, simply do "cscope -b". Please
  259. refer to the cscope man page for more details.
  260. Assuming you have a cscope database, you need to "add" the database to Vim.
  261. This establishes a cscope "connection" and makes it available for Vim to use.
  262. You can do this in your .vimrc file, or you can do it manually after starting
  263. vim. For example, to add the cscope database "cscope.out", you would do:
  264. :cs add cscope.out
  265. You can double-check the result of this by executing ":cs show". This will
  266. produce output which looks like this:
  267. # pid database name prepend path
  268. 0 28806 cscope.out <none>
  269. Note:
  270. Because of the Microsoft RTL limitations, Win32 version shows 0 instead
  271. of the real pid.
  272. Once a cscope connection is established, you can make queries to cscope and
  273. the results will be printed to you. Queries are made using the command
  274. ":cs find". For example:
  275. :cs find g ALIGN_SIZE
  276. This can get a little cumbersome since one ends up doing a significant
  277. amount of typing. Fortunately, there are ways around this by mapping
  278. shortcut keys. See |cscope-suggestions| for suggested usage.
  279. If the results return only one match, you will automatically be taken to it.
  280. If there is more than one match, you will be given a selection screen to pick
  281. the match you want to go to. After you have jumped to the new location,
  282. simply hit Ctrl-T to get back to the previous one.
  283. ==============================================================================
  284. 5. Limitations *cscope-limitations*
  285. Cscope support for Vim is only available on systems that support these four
  286. system calls: fork(), pipe(), execl(), waitpid(). This means it is mostly
  287. limited to Unix systems.
  288. Additionally Cscope support works for Win32. For more information and a
  289. cscope version for Win32 see:
  290. http://iamphet.nm.ru/cscope/index.html
  291. The DJGPP-built version from http://cscope.sourceforge.net is known to not
  292. work with Vim.
  293. Hard-coded limitation: doing a |:tjump| when |:cstag| searches the tag files
  294. is not configurable (e.g., you can't do a tselect instead).
  295. ==============================================================================
  296. 6. Suggested usage *cscope-suggestions*
  297. Put these entries in your .vimrc (adjust the pathname accordingly to your
  298. setup): >
  299. if has("cscope")
  300. set csprg=/usr/local/bin/cscope
  301. set csto=0
  302. set cst
  303. set nocsverb
  304. " add any database in current directory
  305. if filereadable("cscope.out")
  306. cs add cscope.out
  307. " else add database pointed to by environment
  308. elseif $CSCOPE_DB != ""
  309. cs add $CSCOPE_DB
  310. endif
  311. set csverb
  312. endif
  313. By setting 'cscopetag', we have effectively replaced all instances of the :tag
  314. command with :cstag. This includes :tag, Ctrl-], and "vim -t". In doing
  315. this, the regular tag command not only searches your ctags generated tag
  316. files, but your cscope databases as well.
  317. Some users may want to keep the regular tag behavior and have a different
  318. shortcut to access :cstag. For example, one could map Ctrl-_ (underscore)
  319. to :cstag with the following command: >
  320. map <C-_> :cstag <C-R>=expand("<cword>")<CR><CR>
  321. A couple of very commonly used cscope queries (using ":cs find") is to
  322. find all functions calling a certain function and to find all occurrences
  323. of a particular C symbol. To do this, you can use these mappings as an
  324. example: >
  325. map g<C-]> :cs find 3 <C-R>=expand("<cword>")<CR><CR>
  326. map g<C-\> :cs find 0 <C-R>=expand("<cword>")<CR><CR>
  327. These mappings for Ctrl-] (right bracket) and Ctrl-\ (backslash) allow you to
  328. place your cursor over the function name or C symbol and quickly query cscope
  329. for any matches.
  330. Or you may use the following scheme, inspired by Vim/Cscope tutorial from
  331. Cscope Home Page (http://cscope.sourceforge.net/): >
  332. nmap <C-_>s :cs find s <C-R>=expand("<cword>")<CR><CR>
  333. nmap <C-_>g :cs find g <C-R>=expand("<cword>")<CR><CR>
  334. nmap <C-_>c :cs find c <C-R>=expand("<cword>")<CR><CR>
  335. nmap <C-_>t :cs find t <C-R>=expand("<cword>")<CR><CR>
  336. nmap <C-_>e :cs find e <C-R>=expand("<cword>")<CR><CR>
  337. nmap <C-_>f :cs find f <C-R>=expand("<cfile>")<CR><CR>
  338. nmap <C-_>i :cs find i ^<C-R>=expand("<cfile>")<CR>$<CR>
  339. nmap <C-_>d :cs find d <C-R>=expand("<cword>")<CR><CR>
  340. nmap <C-_>a :cs find a <C-R>=expand("<cword>")<CR><CR>
  341. " Using 'CTRL-spacebar' then a search type makes the vim window
  342. " split horizontally, with search result displayed in
  343. " the new window.
  344. nmap <C-Space>s :scs find s <C-R>=expand("<cword>")<CR><CR>
  345. nmap <C-Space>g :scs find g <C-R>=expand("<cword>")<CR><CR>
  346. nmap <C-Space>c :scs find c <C-R>=expand("<cword>")<CR><CR>
  347. nmap <C-Space>t :scs find t <C-R>=expand("<cword>")<CR><CR>
  348. nmap <C-Space>e :scs find e <C-R>=expand("<cword>")<CR><CR>
  349. nmap <C-Space>f :scs find f <C-R>=expand("<cfile>")<CR><CR>
  350. nmap <C-Space>i :scs find i ^<C-R>=expand("<cfile>")<CR>$<CR>
  351. nmap <C-Space>d :scs find d <C-R>=expand("<cword>")<CR><CR>
  352. nmap <C-Space>a :scs find a <C-R>=expand("<cword>")<CR><CR>
  353. " Hitting CTRL-space *twice* before the search type does a vertical
  354. " split instead of a horizontal one
  355. nmap <C-Space><C-Space>s
  356. \:vert scs find s <C-R>=expand("<cword>")<CR><CR>
  357. nmap <C-Space><C-Space>g
  358. \:vert scs find g <C-R>=expand("<cword>")<CR><CR>
  359. nmap <C-Space><C-Space>c
  360. \:vert scs find c <C-R>=expand("<cword>")<CR><CR>
  361. nmap <C-Space><C-Space>t
  362. \:vert scs find t <C-R>=expand("<cword>")<CR><CR>
  363. nmap <C-Space><C-Space>e
  364. \:vert scs find e <C-R>=expand("<cword>")<CR><CR>
  365. nmap <C-Space><C-Space>i
  366. \:vert scs find i ^<C-R>=expand("<cfile>")<CR>$<CR>
  367. nmap <C-Space><C-Space>d
  368. \:vert scs find d <C-R>=expand("<cword>")<CR><CR>
  369. nmap <C-Space><C-Space>a
  370. \:vert scs find a <C-R>=expand("<cword>")<CR><CR>
  371. ==============================================================================
  372. 7. Cscope availability and information *cscope-info*
  373. If you do not already have cscope (it did not come with your compiler
  374. license or OS distribution), then you can download it for free from:
  375. http://cscope.sourceforge.net/
  376. This is released by SCO under the BSD license.
  377. In Solaris 2.x, if you have the C compiler license, you will also have
  378. cscope. Both are usually located under /opt/SUNWspro/bin
  379. There is source to an older version of a cscope clone (called "cs") available
  380. on the net. Due to various reasons, this is not supported with Vim.
  381. The cscope interface/support for Vim was originally written by
  382. Andy Kahn <ackahn@netapp.com>. The original structure (as well as a tiny
  383. bit of code) was adapted from the cscope interface in nvi.
  384. *cscope-win32*
  385. For a cscope version for Win32 see (seems abandoned):
  386. https://code.google.com/archive/p/cscope-win32/
  387. Win32 support was added by Sergey Khorev <sergey.khorev@gmail.com>. Contact
  388. him if you have Win32-specific issues.
  389. vim:tw=78:ts=8:noet:ft=help:norl: