`
joerong666
  • 浏览: 410387 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

在Emacs中使用CEDET

 
阅读更多

By:             潘云登

Date:          2009-7-8

Email:         intrepyd@gmail.com

Homepage:http://blog.csdn.net/intrepyd

Copyright: 该文章版权由潘云登所有。可在非商业目的下任意传播和复制。

对于商业目的下对本文的任何行为需经作者同意。 

 


写在前面

 

 

 

CEDETCollection of Emacs Development Environment Tools的缩写,用以将Emacs改装成高级的程序开发工具。它提供了丰富多彩的功能,如工程管理,智能补全,代码生成,UML图等等。这里,介绍一些平时编程中最常使用的三个功能:代码补全,查找定义和查找引用。

本文主要参考:CEDET主页,《A Gentle introduction to Cedet》,以及曹乐的《Emacs下用C/C++编程》。

 


安装 CEDET

 

 

CEDET主页上下载源码包,解压到任意地方,如~/.emacs.d/site-lisp。在终端下,进入解压后的文件夹,执行下面的命令,进行编译。

 

make EMACS=emacs

 

.emacs文件中加入如下内容,加载CEDET

 

(load-file "~/.emacs.d/site-lisp/cedet-1.0pre6/common/cedet.el")

 

 


配置 CEDET

 

 

这里,将要使用的三个功能是通过执行CEDET定义的不同命令来完成的,所以往.emacs配置文件中加入的主要是这些命令的按键绑定。另外,打开了一个代码折叠功能,对阅读较长文件略有帮助。现在,一并给出需要在.emacs中添加的内容,相关作用以注释形式给出,稍后再详细说明。

 

;;;; 具体说明可参考源码包下的INSTALL文件,或《A Gentle introduction to Cedet

;; Enabling Semantic (code-parsing, smart completion) features

;; Select one of the following:

;;(semantic-load-enable-minimum-features)

;;(semantic-load-enable-code-helpers)

;;(semantic-load-enable-gaudy-code-helpers)

(semantic-load-enable-excessive-code-helpers)

;;(semantic-load-enable-semantic-debugging-helpers)

 

;;;; 使函数体能够折叠或展开

;; Enable source code folding

(global-semantic-tag-folding-mode 1)

 

;; Key bindings

(defun my-cedet-hook ()

  (local-set-key [(control return)] 'semantic-ia-complete-symbol)

  (local-set-key "/C-c?" 'semantic-ia-complete-symbol-menu)

  (local-set-key "/C-cd" 'semantic-ia-fast-jump)

  (local-set-key "/C-cr" 'semantic-symref-symbol)

  (local-set-key "/C-cR" 'semantic-symref))

(add-hook 'c-mode-common-hook 'my-cedet-hook)

 

;;;; 当输入"."">"时,在另一个窗口中列出结构体或类的成员

(defun my-c-mode-cedet-hook ()

  (local-set-key "." 'semantic-complete-self-insert)

  (local-set-key ">" 'semantic-complete-self-insert))

(add-hook 'c-mode-common-hook 'my-c-mode-cedet-hook)

 

对于五种模式,后一种总是包含前一种的功能。其具体作用不甚了解,唯一可以观察到的是,当光标停留在函数调用上时,如printf能够显示函数原型。semantic-load-enable-gaudy-code-helperssemantic-load-enable-excessive-code-helpers的区别在于,前者将函数原型显示在缓冲区顶部,后者显示在状态栏。

    代码补全,使用semantic-ia-complete-symbolsemantic-ia-complete-symbol-menu命令,后者以弹出菜单形式显示所有可能的选项。这里的补全主要是针对函数名和变量名。

    查找定义和引用,分别使用semantic-ia-fast-jumpsemantic-symref-symbolsemantic-symref命令。semantic-symref-symbol请求输入要查找的符号,semantic-symref则查找光标所在处的符号。之前使用cscope完成此类工作(可参考《Cscopeemacs中的配置与使用》)。cscope常用来阅读大型工程的源码,如linux内核。相比之下,semantic更加轻便,无须额外的数据库文件,更适于平时的编程工作。它可以查找到库头文件中的函数声明。遗憾的是,无法查找到上层文件夹中的内容。目前没有找到解决办法,先将就用吧:)

 


更强大的代码补全

 

 

semantic的补全基于编程语义,emacs自带的hippie-expand则提供更为强大的编辑补全功能。这里,直接将曹乐的配置内容添加到.emacs文件中。

 

;;;;自动补齐策略

(defun my-indent-or-complete ()

   (interactive)

   (if (looking-at "//>")

          (hippie-expand nil)

          (indent-for-tab-command))

)

 

(global-set-key [(control tab)] 'my-indent-or-complete)

 

(autoload 'senator-try-expand-semantic "senator")

(setq hippie-expand-try-functions-list

          '(

              senator-try-expand-semantic

                   try-expand-dabbrev

                   try-expand-dabbrev-visible

                   try-expand-dabbrev-all-buffers

                   try-expand-dabbrev-from-kill

                   try-expand-list

                   try-expand-list-all-buffers

                   try-expand-line

        try-expand-line-all-buffers

        try-complete-file-name-partially

        try-complete-file-name

        try-expand-whole-kill

        )

)

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics