Using the GNU Global in Emacs

Source: Internet
Author: User
Tags tag name

backgroundwhen I usually write C code with Emacs, I often need to jump the code, the main requirement is the function definition of the jump, aCall lookup for a body function, a definition jump for a struct, and a jump to a specific item in a struct, the GNU global canfully satisfied with my needs, so the people who are accustomed to Emacs can throw the source insight away. about GNU GlobalGNU Global is known as GNU Global Source code tagging system and is officially defined as GNU Global as aenvironment, such as in the emacs,vi,less Viewer,bash shell or even in a Web browser. You can find local variables, functions, macros, structs, classes, and easily jump to the location you want through GNU global, making large itemsGnul Global can contain a number of subdirectories, #ifdef语句和许多main函数, which is a bit more commonly used ctagsand Etags, but the following two points are more powerful than the two tag tools: GNU Global .
    • And the editor does not have a strong binding relationship, so it can be easily used in a variety of editing environment, whether the loyal users of Emacs or Vim fans can use it to do the code jump
    • The ability to find definitions and references is powerful enough for code reading.
GNU Global has a strong cross-platform capability that can be used both in Linux and BSD systems and in Windows. GNU Global has the following features:
    • Built-in 6 syntactic parser (definitions and references) C,C++,YACC,JAVA,PHP4 and assemblies
    • Ctags's syntax parsing plugin supports 25 languages (definitions and references) 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
    • Can work in the same way in a variety of editing environments, such as
      • Shell command line
      • Bash Shell
      • VI Editor (VIM)
      • Less Viewer
      • Emacs Editor (EMACS,MULE,XEMACS)
      • Web browser
      • Doxygen Document System
  • Find local variables quickly with a special symbol table
  • Not only can you find a definition but also find references
  • Allow duplicates of tags
  • Give a matching path
  • Default Hierarchical Lookups
  • It's not just code-level lookups, you can find it in the library.
  • Create a complete list
  • Supports multiple format outputs
  • Allows you to specify a tag file
  • Support for POSIX 1003.2 regular expressions
  • Support Idutils as an external search engine
  • The generated tag file is independent of the various architectures
  • Support to update tag files at any time
  • Syntax parsing plugin to support new languages
  • Support for Custom gtags.conf
  • Use compressed format to save space
  • Support C/S environment (TRAMP)
  • Default ignores executables, hidden files, and special files
  • Built-in Cscope program (Gtags-cscope)
  • Built-in grep command (using-g)
  • Able to handle cyclic chains very well
GNU Global usesusing global at the command line, you can check the FAQ before you start using
$more/your/gtags/path/faq
first we need to use the Gtags command to generate a tag file for the code tree, such as when I want to browse the kernel 2.4code, you only need to execute the following command
$CD ~/code/kernel/$gtags-V
after execution, you will find that 3 tag files are generated in the kernel directory, respectively, Gpath,grtags,gtags,Gtags is a database that is defined, Grtags is a referenced database, Gpath is a database of paths.  basic usage, add the following is our directory tree
/home/user/| |-root/<-The ROOT of the source tree (Gtags,grtags,...)    |                       |-README ... +---------------+ | |    The function of|                       |  +---------------+ |-dir1/|    |  |  |-filea.c ... +---------------+ |                    |    |main () {|  |                    |       |  Func1 (); | |                    |       |  Func2 (); | |                    |              |}    |  |                    |  +---------------+    |    |  |                       |-FILEB.C ... +---------------+ |    |func1 () {...} |                       |       +---------------+ |-dir2/|                            |-filec.c ... +---------------+ | #ifdef X |                            |func2 () {i++;}|                            | #else |                            |func2 () {i--;}|                            | #endif |                 |func3 () {|           |              Func1 (); | |}                            | +---------------+
At this point we execute the gtags command in the root directory to generate the tag file, you can use the global command to search the coderelated functions, it is important to be aware that only the directory in which the tag file is generated and its subdirectories are searched:
$ cd/home/user/root$ Global FUNC1DIR1/FILEB.C            # FUNC1 () is defined in fileb.c$ CD dir1$ global FUNC1FILEB.C                 # rel ative path from dir1$ CD. /dir2$ Global func1. /DIR1/FILEB.C         # relative path from DIR2
Use the-r option of global to get related references:
$ global-r Func2. /DIR1/FILEA.C         # FUNC2 () is referred from FILEA.C
global support for searching using regular expressions:
$ cd/home/user/root$ Global ' func[1-3] ' dir1/fileb.c            # func1, Func2 and func3 are MATCHEDDIR2/FILEC.C
Global uses the-X option to get more details:
$ global func2dir2/filec.c$ global-x func2func2              2 dir2/filec.c       Func2 () {i++;} Func2              4 dir2/filec.c       Func2 () {i--;}
The absolute path can be obtained when global uses the-a option:
$ global-a FUNC1/HOME/USER/ROOT/DIR1/FILEB.C
Global uses the-S option to search for symbols that are not defined in Gtags:
$ global-xs XX                  1 dir2/filec.c #ifdef X
Global uses the-G option to search for the specified pattern:
$ Global-xg ' #ifdef ' #ifdef             1 dir2/filec.c #ifdef X
the-O option for global indicates that search is only in text files, and-o is searched in source and text files,The- l option searches only in the current directory, using the-p option to search for a specific mode path; If you want to search for related symbols in a library, you need to perform gtags in each Gtagslibpathcommand:
$ PWD/DEVELOP/SRC/MH                 # This is a source project$ gtags$ ls g*tagsgrtags  gtags$ global mhluip/mhlsbr.c                    # MHL () is found$ global strlen                 # strlen () are not found$ (cd/usr/src/lib; gtags)      # library source$ (Cd/usr/src/sys; gtag s)      # kernel source$ export gtagslibpath=/usr/src/lib:/usr/src/sys$ global strlen. /.. /.. /USR/SRC/LIB/LIBC/STRING/STRLEN.C  # found in library$ global access: /.. /.. /USR/SRC/SYS/KERN/VFS_SYSCALLS.C   # found in kernel
$ ln-s/usr/src/lib. $ ln-s/usr/src/sys. $ gtags$ Global strlenlib/libc/string/strlen.c$ Global ACCESSSYS/KERN/VFS_SYSC Alls.c
If you forget the name of the symbol you are searching for, you can use the-C option to complete:
$ Global-c Kmem # maybe K.. K.. kmem. Kmem_allockmem_alloc_pageablekmem_alloc_waitkmem_freekmem_free_wakeupkmem_initkmem_mallockmem_suballoc # is What I need!$ global kmem_suballoc. /VM/VM_KERN.C $ funcs () > {> local cur> cur=${comp_words[comp_cword]}> compreply= (' global-c $cur ') >}$ com Plete-f funcs global$ Global kmem_<tab><tab>kmem_alloc kmem_alloc_wait kmem_initkmem_alloc_nofault kmem_ Free kmem_mallockmem_alloc_pageable kmem_free_wakeup kmem_suballoc$ global kmem_s<tab>$ Global kmem_suballoc. /vm/vm_kern.c
The use of GNU Global in Emacs
    • Installation configuration Emacs
$HOME/.emacs+------------------------------------------------------| (Setq Load-path (cons "/home/owner/global" Load-path)) | (AutoLoad ' Gtags-mode "gtags" "" t)
(Global-set-key (kbd "m-<f1>") ' Gtags-find-file)
(Global-set-key (kbd "m-<f2>") ' Gtags-find-tag)
(Global-set-key (kbd "m-<f3>") ' Gtags-find-rtag)
(Global-set-key (kbd "m-<f4>") ' Gtags-find-symbol)
(Global-set-key (kbd "m-<f5>") ' Gtags-find-with-grep)
    • After installation, you can use the following command in Emacs via M-x.
Display the current screen in the browser
Jump to the input file
Search by grep command
Search for references in the tag file
Searching for symbols in the tag file
Search for definitions in the tag file
Gets the tag name from the current expression and jumps
Jump to the current position as tag
Open tag jump in another window
Search for files and query

Tag name completion
Open Gtags Mode
Displays the tag of the current file
Move from stack to previous point
Select a tag from the tag list
Select a tag
Select tag from an event
Select tag in the other window
Access the tag file root directory
Gtags-display-browser Gtags-find-filegtags-find-pattern Gtags-find-rtaggtags-find-symbolgtags-find-taggtags-find-tag-by-event Gtags-find-tag-from-heregtags-find-tag-other-window gtags-find-with-grepgtags-find-with-idutils Gtags-make-complete-listgtags-mode Gtags-parse-filegtags-pop-stack Gtags-select-modegtags-select-tag Gtags-select-tag-by-eventgtags-select-tag-other-window Gtags-visit-rootdir

Using the GNU Global in Emacs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.