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.
What is the difference (e.g. in terms of features) between
etags
,ebrowse
,exuberant ctags
,cscope
,GNU Global
andGTags
? What do I need to do to use them in Emacs?Do I need semantic/senator (CEDET) if I want to use tags to navigate/autocomplete symbols?
What does semantic bring to the table on top of these different tag utilities? How does it interface with these tools?
I haven't actually checked, but according to CEDET manual (http://www.randomsample.de/cedetdocs/common/cedet/CScope.html):
semantic can use CScope as a back end for database searches. To enable it, use:
This will enable the use of cscope for all C and C++ buffers.
CScope will then be used for project-wide searches as a backup when pre-existing semantic database searches may not have parsed all your files.
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
andctags
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 ofctags
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: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 aboutctags
. 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:
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
andcscope
, 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
andebrowse
, but if memory serves me they made use of etags.TAGS files contain definitions
A
TAGS
file contains a list of where functions and classes are defined. It is usually placed in the root of a project and looks like this:This enables Emacs to find definitions. Basic navigation is built-in with
find-tag
, butetags-select
provides a nicer UI when there are multiple matches.You can also uses TAGS files for code completion. For example, company's etags backend uses TAGS files.
TAGS files can be built by different tools
ctags
(formerly known as 'universal ctags' or 'exuberant ctags') can generate TAGS files and supports the widest range of languages. It is actively maintained on github.Emacs ships with two programs that generate TAGS files, called
etags
andctags
. Emacs'ctags
is justetags
with the same CLI interface as universal ctags. To avoid confusion, many distros rename these programs (e.g.ctags.emacs24
on Debian).There are also language specific tools for generating TAGS files, such as
jsctags
andhasktags
.Other file formats
ebrowse
is a C program shipped with Emacs. It indexes C/C++ code and generates aBROWSE
file. ebrowse.el provides the usual find definition and completion. You can also open theBROWSE
file directly in Emacs to get an overview of the classes/function defined a codebase.GNU Global has its own database format, which consists of a
GTAGS
,GRTAGS
andGPATH
file. You can generate these files with thegtags
command, which parses C/C++ code. For other languages, GNU Global can read files generated by universal ctags.GNU Global also provides a CLI interface for asking more sophisticated questions, like 'where is this symbol mentioned?'. It ships with an Emacs package gtags.el, but ggtags.el is also popular for accessing GNU Global databases.
Cscope is similar in spirit to GNU Global: it parses C/C++ into its own database format. It can also answer questions like 'find all callers/callees of this funciton'.
See also this HN discussion comparing global and cscope.
Client/Server projects
rtags parses and indexes C/C++ using a persistent server. It uses the clang parser, so it handles C++ really well. It ships with an Emacs package to query the server.
google-gtags was a project where a large TAGS file would be stored on a server. When you queried the server, it would provide a subset of the TAGS file that was relevant to your search.
Semantic (CEDET)
Semantic is a built-in Emacs package that contains a parser for C/C++, so it can find definitions too. It can also import data from TAGS files, csope databases, and other sources. CEDET also includes IDE style functionality that uses this data, e.g. generating UML diagrams of class hierarchies.
[answer updated from shigio's]
I'll try to add some explanations to part 1 of the question.
What is it?
TAGS
file which is the tag file format for Emacs. You can use an Etags file withetags.el
which is part of Emacs.Ctags
is the generic term for anything that can generate atags
file, which is the native tag file format for Vi. Universal Ctags (akaUCtags
, formerly Exuberant Ctags) can also generate Etags with the-e
option.cscope.in.out
,cscope.out
,cscope.po.out
) and TUI. Cscope support is built-in to Vim; you can use Cscope from Emacs using the xcscope.el package. There are also Cscope-based GUIs.Gtags
) is yet another source code tagging system (with significant differences--see next section), in that it also generates tag files.Comparison
grep
-like search engine.less
(pager), Doxygen, and any web browser.gtags.el
via the GLOBAL package, but there are also many other elisp extensions, including xgtags.el, ggtags.el, anything-gtags.el, helm-gtags.el.Combination
You can combine Universal Ctags' rich language support with Gtags' database facility and numerous extensions by using Ctags as a GLOBAL plug-in parser:
Note again that if you use Ctags as the parser for your Gtags, you lose the ability to treat references (e.g., variable usage, function calls) which Gtags would otherwise provide. Essentially, you trade off Gtags' reference tracking for Ctags' greater built-in language support.
You can also use Cscope as a client of Gtags:
gtags-cscope
.Good luck!
I'll try to add some explanations to 1.
What is it?
Comparison
Combination
You can combine Exuberant Ctags's rich language support and GNU GLOBAL's database facility by using ctags as a plug-in parser of GLOBAL.
Try the followings: (requires GLOBAL-6.0, Exuberant Ctags-5.5 or later respectively)
Building GNU GLOBAL:
Usage:
(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.
With the combinations, you can use cscope for 41 languages.
Good luck!