用emacs 阅读 c/c++ 代码

在emacs编程中有以下需求

  • 从调用一个函数的地方跳转到函数的定义的地方
  • 或是反过来从函数定义的地方列出所有调用这个函数的地方

实现办法

需要安装以下软件

  • gnu global(阅读源代码的工具)官网
  • emacs的gtags插件
  • 启动emacs后,输入下面的命令,来启动gtags的功能
M-x ggtags-mode
gnu global安装
apt-get install global

emacs的gtags插件安装

M-x list-packages

找到ggtags后,鼠标点击,再点安装

使用方法

1,在存放代码的根目录执行:

$ gtags

执行后,会发现在生成了3个文件, GPATH GRTAGS GTAGS

2,启动emacs

3,启动emacs后,输入下面的命令,来启动gtags的功能

M-x ggtags-mode

每打开一个文件都输入ggtags-mode,太麻烦了,所以在.emacs文件里加一个钩子,当打开c,c++,java的源文件时,自动启动ggtags-mode

(add-hook 'c-mode-common-hook
          (lambda ()
            (when (derived-mode-p 'c-mode 'c++-mode 'java-mode)
              (ggtags-mode 1))))

4,使用下面的函数就可以实现,查看代码的各种需求了。

ggtags-find-tag-dwim

   Find a tag by context.

   If point is at a definition tag, find references, and vice versa.
   If point is at a line that matches ``ggtags-include-pattern``, find
   the include file instead.

   To force finding a definition tag, call it with a prefix (``C-u``).

ggtags-find-tag-mouse

   Like ``ggtags-find-tag-dwim`` but suitable for binding to mouse
   events.

ggtags-find-definition

   Find definition tags. With ``C-u`` ask for the tag name with
   completion.

ggtags-find-reference

   Find reference tags. With ``C-u`` ask for the tag name with completion.

ggtags-find-other-symbol

   Find tags that have no definitions. With ``C-u`` ask for the tag
   name with completion.

ggtags-find-tag-regexp

   Find definition tags matching a regexp. By default it lists all
   matching tags in the project. With ``C-u`` restrict the lists to a
   directory of choice.

ggtags-idutils-query

   Use idutils to find matches.

ggtags-grep

   Grep for lines matching a regexp. This is usually the slowest.

ggtags-find-file

   Find a file from all the files indexed by ``gtags``.

ggtags-query-replace

   Do a query replace in all files found in a search.

Handling multiple matches
+++++++++++++++++++++++++

When a search finds multiple matches, a buffer named
``*ggtags-global*`` is popped up and ``ggtags-navigation-mode`` is
turned on to facilitate locating the right match.
``ggtags-navigation-mode`` makes a few commands in the
``*ggtags-global*`` buffer globally accessible:

``M-n``

   Move to the next match.

``M-p``

   Move to the previous match.

``M-}``

   Move to next file.

``M-{``

   Move to previous file.

``M-=``

   Move to the file where navigation session starts.

``M-<``

   Move to the first match.

``M->``

   Move to the last match.

``C-M-s`` or ``M-s s``

   Use ``isearch`` to find the match.

``RET``

   Found the right match so exit navigation mode. Resumable by ``M-,``
   (``tags-loop-continue``).

``M-*``

   Abort and go back to the location where the search was started.

Miscellaneous commands
++++++++++++++++++++++

Commands are available from the ``Ggtags`` menu in ``ggtags-mode``.

ggtags-prev-mark

   Move to the previously (older) visited location. Unlike ``M-*``
   this doesn't delete the location from the tag ring.

ggtags-next-mark

   Move to the next (newer) visited location.

ggtags-view-tag-history

   Pop to a buffer listing all visited locations from newest to
   oldest. The buffer is a next error buffer and works with standard
   commands ``next-error`` and ``previous-error``. In addition ``TAB``
   and ``S-TAB`` move to next/prev entry, and ``RET`` visits the
   location. ``M-n`` and ``M-p`` move to and display the next/previous
   entry.

ggtags-view-search-history

   View or re-run past searches as kept in
   ``ggtags-global-search-history``.

ggtags-kill-file-buffers

   Kill all file-visiting buffers of current project.

ggtags-toggle-project-read-only

   Toggle opening files in ``read-only`` mode. Handy if the main
   purpose of source navigation is to read code.

ggtags-visit-project-root

   Open the project root directory in ``dired``.

ggtags-delete-tags

   Delete the GTAGS, GRTAGS, GPATH and ID files of current project.

ggtags-explain-tags

  Explain how each file is indexed in current project.

ggtags-browse-file-as-hypertext

   Use ``htags`` to generate HTML of the source tree. This allows
   browsing the project in a browser with cross-references.

使用举例

1,从调用一个函数的地方跳转到函数的定义的地方

M-x ggtags-find-definition
对应的快捷键:M-.

2,从调用一个函数的地方跳转到函数的定义的地方后,返回到调用函数的地方

M-x ggtags-prev-mark
对应的快捷键:M-,

3,或是反过来从函数定义的地方列出所有调用这个函数的地方

M-x ggtags-find-reference
对应的快捷键:M-]

4,查看导航过的历史记录

M-x ggtags-view-tag-history
对应的快捷键:C-c M-h

语法高亮插件

参考:https://zhuanlan.zhihu.com/p/26471685

1,安装symbol-overlay
2,配置

(require 'symbol-overlay)
(global-set-key (kbd "M-i") 'symbol-overlay-put)
(global-set-key (kbd "M-n") 'symbol-overlay-switch-forward)
(global-set-key (kbd "M-p") 'symbol-overlay-switch-backward)
(global-set-key (kbd "<f7>") 'symbol-overlay-mode)
(global-set-key (kbd "<f8>") 'symbol-overlay-remove-all)

3,使用

"i" -> symbol-overlay-put                ; 高亮或取消高亮当前symbol
"n" -> symbol-overlay-jump-next          ; 跳转到下一个位置
"p" -> symbol-overlay-jump-prev          ; 跳转到上一个位置
"w" -> symbol-overlay-save-symbol        ; 复制当前symbol
"t" -> symbol-overlay-toggle-in-scope    ; 切换高亮范围到作用域
"e" -> symbol-overlay-echo-mark          ; 撤销上一次跳转
"d" -> symbol-overlay-jump-to-definition ; 跳转到定义
"s" -> symbol-overlay-isearch-literally  ; 切换为isearch并搜索当前symbol
"q" -> symbol-overlay-query-replace      ; 查找替换当前symbol
"r" -> symbol-overlay-rename             ; 对symbol直接重命名
posted @ 2018-06-20 16:21  小石王  阅读(1132)  评论(0编辑  收藏  举报