GTags in Emacs

Prologue

I usually code in Python and when I have a lot of functions in different files in a project then it is desirable to jump to the function definition from its identifier at point. In Emacs, Elpy has (elpy-goto-definition) with keybinding Meta-., that sometimes works on local files and doesn't most of the time on remote files. There are some other good options like elpy-rgrep-symbol, rgrep but they are not that efficient. I have also tried lsp-mode with mspyls which works good in local but always complains about server-time-elapsed over tramp. As I work mostly on my servers, it is really important for me to get a reliable tool for this feature.

I came across gnu-global on some online platform which is widely used by C-programmers for similar use-cases as of mine. The main idea is to create TAG files of your source codes and feed them to Emacs for navigation around the project. This tool works for python too but with some extra plugins that I have discussed below. I followed the gtags-spacemacs guide which works for my vanilla-Emacs too. To make it work over tramp I had to install gnu-global on the server and this article summerizes those steps.

Install gnu-global on server

If you have the admin rights on the server then you can follow the same instruction as of gtags-spacemacs and everything works fine. Without the sudo rights, you need to do the following steps to install it properly. Before installing gnu-global, first I had to install all the dependencies like,

  1. ctags:

    - git clone https://github.com/universal-ctags/ctags.git
    - bash autogen.sh
    - ./configure --prefix=/path/to/install
    - keep the path-to-install same for all the rest of the packages
    - make
    - make install
    
  1. gperf:

    - wget https://ftp.gnu.org/pub/gnu/gperf/gperf-3.1.tar.gz
    - ./configure --prefix=/path/to/install (keep it same as of ctags)
    - make
    - make install
    
  1. libtool:

    - wget https://ftp.gnu.org/gnu/libtool/libtool-2.4.2.tar.gz (this version is important)
    - ./configure --prefix=/path/to/install (keep it same as of ctags)
    - make
    - make install
    
    
  2. texinfo:

    - wget https://ftp.gnu.org/gnu/texinfo/texinfo-6.7.tar.xz
    - ./configure --prefix=/path/to/install (keep it same as of ctags)
    - make
    - make install
    
    

I added the installation path (path-to-install/bin) to the .bashrc to access them from the terminal directly.

export PATH="/projappl/project_2001838/bin:$PATH"

Finally, install the gnu-global with following three commands.

- ./configure --with-exuberant-ctags=/path/to/install/bin/ctags --prefix=/path/to/install/
- make
- make install

Creating TAGs and accessing from Emacs

  • Once gnu-global is installed, I can go to any project directory and type gtags --gtagslabel=pygments to create gtags. In order to access them from the Emacs, I installed helm-gtags.
  • Before using helm-gtags over tramp, I need to tell the tramp the path of gnu-global on the server. I have done that by adding (add-to-list 'tramp-remote-path "/path/to/install/bin") to my init.el. Now you are ready to go. The screenshot shows it in action. We can go to any function identifier and then it can take us to the function definition even defined in another file.

demo.gif

Author: Soumya Tripathy

Created: 2020-12-13 Sun 17:27

Validate