Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctags

I work on C++ projects, and I went through Alex Ott's guide to CEDET and other threads about tags in StackOverflow, but I am still confused about how Emacs interfaces with these different tag systems to facilitate autocompletion, the looking up of definitions, navigation of source code base or the previewing of doc-strings.

  1. What is the difference (e.g. in terms of features) between etags, ebrowse, exuberant ctags, cscope, GNU Global and GTags? What do I need to do to use them in Emacs?

  2. Do I need semantic/senator (CEDET) if I want to use tags to navigate/autocomplete symbols?

  3. What does semantic bring to the table on top of these different tag utilities? How does it interface with these tools?

like image 826
Amelio Vazquez-Reina Avatar asked Oct 16 '12 19:10

Amelio Vazquez-Reina


People also ask

What is the difference between Ctags and Cscope?

ctags can be used to take you to the definition of a variable (e.g., a function, variable, or macro). cscope can be used to take you to the call site of a definition (e.g., all function calls, all variable uses, all macro uses).

How do I use tags in Emacs?

' ( 'find-tag' ) – find a tag, that is, use the Tags file to look up a definition. If there are multiple tags in the project with the same name, use ` C-u M-. ' to go to the next match. 'M-x find-tag-other-window' – selects the buffer containing the tag's definition in another window, and move point there.

What is Gtags?

Gtags is used to create tag files for global(1). Gtags recursively collects source files under the current directory, picks up symbols and writes the cross-reference data into the tag files (´GTAGS´, ´GRTAGS´ and ´GPATH´). By default, gtags picks up C, Yacc, assembly language, Java, C++ and PHP source files.


2 Answers

That's as a good question as I've recently read here, so I'll try explain the difference in more detail:

Point 1:

etags and ctags both generate an index (a.k.a. tag/TAGS) file of language objects found in source files that allows these items to be quickly and easily located by a text editor or other utility. A tag signifies a language object for which an index entry is available (or, alternatively, the index entry created for that object). The tags generated by ctags are richer in terms of metadata, but Emacs cannot interpret the additional data anyways, so you should consider them more or less the same (the main advantage of ctags would be its support for more languages). The primary use for the tags files is looking up class/method/function/constant/etc declaration/definitions.

cscope is much more powerful beast (at least as far as C/C++ and Java are concerned). While it operates on more or less the same principle (generating a file of useful metadata) it allows you do some fancier things like find all references to a symbol, see where a function is being invoked, etc (you can find definitions as well).

To sum it up:

ctags one allows you to navigate to symbol declaration/definitions (what some would call a one-way lookup). ctags is a general purpose tool useful for many languages.

On the other hand (as mentioned on the project's page) cscope allows you to:

  • Go to the declaration of a symbol
  • Show a selectable list of all references to a symbol
  • Search for any global definition
  • Functions called by a function
  • Functions calling a function
  • Search for a text string
  • Search for a regular expression pattern
  • Find a file
  • Find all files including a file

It should come as no surprise to anyone at this point, that when I deal with C/C++ projects I make heavy use of cscope and care very little about ctags. When dealing with other languages the situation would obviously be reversed.

Point 2.

To have intelligent autocompletion you need a true source code parser (like semantic), otherwise you won't know the types of the objects (for instance) in your applications and the methods that can be invoked on them. You can have an autocompletion based on many different sources, but to get the best results you'll ultimately need a parser. Same goes for syntax highlighting - currently syntax highlighting in Emacs major modes is based simply on regular expressions and that's very fragile and error prone. Hopefully with the inclusion of semantic in Emacs 23.2 (it used to be an external package before that) we'll start seeing more uses for it (like using it to analyse a buffer source code to properly highlight it)

Since Emacs 24.1 semantic is usable from the Emacs completion framework. The easiest way to test it is to open up a C source code file and typing M-TAB or C-M-i and watch as semantic automagically completes for you. For languages where semantic is not enabled by default, you can add it the following line to your major mode hook of choice:

(add-to-list 'completion-at-point-functions 'semantic-completion-at-point-function) 

Point 3.

semantic brings true code awareness (for the few languages it currently supports) and closes the gap between IDEs and Emacs. It doesn't really interface with tools like etags and cscope, but it doesn't mean you cannot use them together.

Hopefully my explanations make sense and will be useful to you.

P.S. I'm not quite familiar with global and ebrowse, but if memory serves me they made use of etags.

like image 67
Bozhidar Batsov Avatar answered Nov 28 '22 10:11

Bozhidar Batsov


I'll try to add some explanations to 1.

What is it?

  • Etags is a command to generate 'TAGS' file which is the tag file for Emacs. You can use the file with etags.el which is part of emacs package.
  • Ctags is a command to generate 'tags' file which is the tag file for vi. Universal Ctags, the successor of Exuberant Ctags, can generate 'TAGS' file by the -e option, supporting more than 41 programming languages.
  • Cscope is an all-in-one source code browsing tool for C language. It has own fine CUI (character user interface) and tag databases (cscope.in.out, cscope.out, cscope.po.out). You can use cscope from Emacs using xcscope.el which is part of cscope package.
  • GNU GLOBAL is a source code tagging system. Though it is similar to above tools, it differs from them at the point of that it is dependent from any editor, and it has no user interface except for command line. Gtags is a command to generate tag files for GLOBAL (GTAGS, GRTAGS, GPATH). You can use GLOBAL from emacs using gtags.el which is part of GLOBAL package. In addition to this, there are many elisp libraries for it (xgtags.el, ggtags.el, anything-gtags.el, helm-gtags.el, etc).

Comparison

  • Ctags and etags treat only definitions. Cscope and GNU GLOBAL treat not only definitions but also references.
  • Ctags and etags use a flat text tag file. Cscope and GNU GLOBAL use key-value tag databases.
  • Cscope and GNU GLOBAL have a grep like search engine and incremental updating facility of tag files.

Combination

You can combine Universal Ctags's rich language support and GNU GLOBAL's database facility by using ctags as a plug-in parser of GLOBAL.

Try the following: (requires GLOBAL-6.5.3+ and Universal Ctags respectively)

Building GNU GLOBAL:

$ ./configure --with-universal-ctags=/usr/local/bin/ctags $ sudo make install 

Usage:

$ export GTAGSCONF=/usr/local/share/gtags/gtags.conf $ export GTAGSLABEL=new-ctags $ gtags                     # invokes Universal Ctags internally $ emacs -f gtags-mode       # load gtags.el 

(However, you cannot treat references by this method, because ctags don't treat references.)

You can also use cscope as a client of GNU GLOBAL. GLOBAL package includes a command named 'gtags-cscope' which is a port of cscope, that is, it is cscope itself except that it use GLOBAL as a search engine instead of cscope's one.

$ gtags-cscope          # this is GLOBAL version of cscope 

With the combinations, you can use cscope for 41 languages.

Good luck!

like image 39
shigio Avatar answered Nov 28 '22 10:11

shigio