昨天,抽风

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

tubo posted @ 2014年9月03日 00:00 in 未分类 , 942 阅读

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 。

 

meidir 说:
2022年12月06日 04:59

There are some intriguing points in time on this page but I don’t determine if I see these people center to heart. There may be some validity but I most certainly will take hold opinion until I check into it further. Good write-up , thanks and we want a lot more! Added onto FeedBurner in addition 승무패

 

================

 

You need to join in a contest for just one of the greatest blogs online. I am going to suggest this page! 攝影器材

meidir 说:
2023年1月12日 04:52

Hi! I just wanted to ask if you ever have any issues with hackers? My last blog (wordpress) was hacked and I ended up losing a few months of hard work due to no backup. Do you have any solutions to protect against hackers? 바카라사이트

meidir 说:
2023年2月18日 22:36

Hello. Cool article. There is an issue with your web site in internet explorer, and you might want to check this… The browser is the marketplace chief and a big section of people will omit your great writing because of this problem. 슈어맨

meidir 说:
2023年3月04日 04:53

Good – I should definitely say I’m impressed with your web site. I had no trouble navigating through all the tabs and related information. The site ended up being truly easy to access. Nice job. 룸알바

meidir 说:
2023年3月11日 03:32

Hi there. I wanted to ask some thing…is this a wordpress webpage as we are planning to be switching over to WP. Additionally did you make this template all by yourself? Bless you. 메이저바카라

meidir 说:
2023年8月22日 06:09

well of course, everyone loves to get rich but not everyone would love to do hard work,. 插花技巧


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter