您的位置:首页 > 编程语言 > C语言/C++

Emacs 通过 CEDET 实现 Tag 跳转 (C/C++/Python)

2011-09-04 08:49 453 查看

1 Preface

前面曾经介绍过 GNU Global , 也提到了它没有直接支持 python 解析, 想要用它来生成 python 文件的 tags 的话,需要自己编写相应的插件。 此外, GNU Global 对 C++ 的支持程度和 C 相同, 如果两个类从包含了同名的成员函数, 在查找这个成员函数的定义时, global 不会准确的跳到相应的函数上, 还需要我们手动去选择相应的 tag 。

CEDET 是 Emacs 中的一个强大的插件,它提供了基于语义的 Tag 跳转, 该功能对 C/C++, Python, Java 等的支持都非常准确, 并且通过 mru-bookmark-mode 中的 semantic-mrub-switch-tags 提供了在多个 Tag 中来回跳转的功能。 但是, 这个跳转需要用户去指定待跳回的 Tag, 且由于默认跳转到最近一次访问的 tag , 常常会弄的很乱。

2 Solution

新建一个栈,每次通过 semantic-ia-fast-jump 或者 semantic-ia-complete-jump 跳转的时候, 都将原 Tag 信息入栈,而每次想要跳回的时候,都从栈中 pop 出一个,并跳到刚刚 pop 出的 tag 中。

代码如下:

(require 'cedet)

;; Enable code helpers.
(semantic-load-enable-code-helpers)
(global-semantic-mru-bookmark-mode 1)

(defvar mru-tag-stack '()
"Tag stack, when jumping to new tag, current tag will be stored here,
and when jumping back, it will be removed.")

(defun yc/store-mru-tag (pt)
"Store tag info into mru-tag-stack"
(interactive "d")
(let* ((tag (semantic-mrub-find-nearby-tag pt)))
(if tag
(let ((sbm (semantic-bookmark (semantic-tag-name tag)
:tag tag)))
(semantic-mrub-update sbm pt 'mark)
(add-to-list 'mru-tag-stack sbm)
)
(error "No tag to go!")))
)

(defun yc/goto-func (pt)
"Store current postion and call (semantic-ia-fast-jump)"
(interactive "d")
(yc/store-mru-tag pt)
(semantic-ia-fast-jump pt)
)

(defun yc/goto-func-any (pt)
"Store current postion and call (semantic-ia-fast-jump)"
(interactive "d")
(yc/store-mru-tag pt)
(semantic-complete-jump)
)

(defun yc/symref (pt)
(interactive "d")
(yc/store-mru-tag pt)
(semantic-symref))

(defun yc/return-func()
"Return to previous tag."
(interactive)
(if (car mru-tag-stack)
(semantic-mrub-switch-tags (pop mru-tag-stack))
(error "TagStack is empty!")))

(defun setup-program-keybindings()
;;;; Common program-keybindings
(interactive)
(local-set-key "\C-cR" 'yc/symref)
(local-set-key "\C-cb" 'semantic-mrub-switch-tags)
(local-set-key "\C-c\C-j" 'yc/goto-func-any)
(local-set-key "\C-cj" 'yc/goto-func)
(local-set-key [S-f12] 'yc/return-func)
(local-set-key [M-S-f12] 'yc/return-func)
(local-set-key (kbd "C-x SPC") 'yc/store-mru-tag)
)

用法: 参考函数 setup-program-keybindings 。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: