How to setup GNU GLOBAL for Emacs (MAC)

GNU GLOBAL is a source code tagging system that works the same way across diverse environments, such as Emacs editor, Vi editor, Less viewer, Bash shell, various web browsers, etc. (http://www.gnu.org/software/global/)


 

This article is an instruction for installing GNU GLOBAL into your Emacs with MAC OS X.

Step 1 – Install GLOBAL

Users of MAC OS X are able to install GLOBAL simply by using Macports or Homebrew.

For example, if you are using the Macports, type in the command below in terminal.

sudo port install global

Step 2 – Download gtags.el

Download the newest source archive in official site, and unzip it. You should find the file named “gtags.el” in the folder unzipped. Then you can put it wherever you like.

Step 3 – Setup .emacs file

After installing of GLOBAL, we need to setup .emacs file by adding lines below into it.

(setq load-path (cons "path of global" load-path))
(setq load-path (cons "path of gtags" load-path))
(setq load-path (cons "path of gtags.el" load-path))
(autoload 'gtags-mode "gtags" "" t)

The path of global and gtags can be known by typing in “which global” and “which gtags” in terminal.

Optionally, you can add some keybinding for gtags by adding lines below.

(setq gtags-mode-hook
'(lambda ()
(local-set-key "\M-t" 'gtags-find-tag)
(local-set-key "\M-r" 'gtags-find-rtag)
(local-set-key "\M-s" 'gtags-find-symbol)
(local-set-key "\C-t" 'gtags-pop-stack)
))

And by now, you are able to load GLOBAL by M-x gtags-mode in Emacs. By adding lines below, you can make emacs load gtags-mode automatically when c-mode or c++-mode is loaded.

(setq c-mode-hook
'(lambda ()
(gtags-mode 1)))
(setq c++-mode-hook
'(lambda ()
(gtags-mode 1)))

GLOBAL supports Awk, Dos batch, COBOL, C, C++, C#, Erlang, Fortran, Java, JavaScript, Lisp, Lua, Pascal, Perl, PHP, Python, Ruby, Matlab, OCaml, Scheme, Tcl, TeX, Verilog, Vhdl and Vim.

Step 4 – Enjoy yourself

Now you are done with installing and should be able to use gtags-mode in your emacs.

To use it, for example, type in the command below in your terminal.

cd path-where-you-want-to-use-gtags-mode
gtags -v

After doing this, a few files for gtags will be created under target directory. And you can use gtags-mode when you are reading the file of that directory in Emacs.