Emacs 23.2 自带的Cedet的使用(如果自己下载cedet再安装可能会遇到一些麻烦)

1 篇文章 0 订阅
1 篇文章 0 订阅

Blog Transferred to: http://www.kanhua.org


Cedet是Emacs 23.2中自带的,版本和sourceforge上的Cedet相同,但是其中的内 容(主要是函数名之类)有不少的变化。这里以 A Gentle introduction to Cedet 为基础,写的这个文章,结构与之类似,内容上有些地方是翻译,

 

1 什么是Cedet

CEDET 是 Collection of Emacs Development Environment Tools的缩写, 意为"Emacs开发环境工具集",其主要目的是在Emacs中建立一个高级的开发环境。 它主要包括下列组件:

  • Semantic -— 多种编程语言的语法分析的基础组件。
  • SemanticDB-—包含在Semantic中的一个数据库,用于保存代码的语法、接口等等信息。
  • Senator -— 通过Semantic提取出来的信息构成的代码文件中的navegation。
  • Srecode -— 代码生成组件。
  • EDE -— 提供工程管理相关功能;
  • Speedbar -— 用于显示当前Buffer的侧边栏。
  • Eieio is a library, implementating CLOS-like (Common Lisp Object System) infrastructure for Emacs Lisp;
  • Cogre is a library for drawing of UML-like diagrams in Emacs buffer, with basic integration with Semantic.

其中,最后面这两个我没怎么用过。

2 Cedet的安装和启用

Emacs 23.2 中已经自带了Cedet,所以无需再单独安装,直接启用即可:

(require 'cedet)

如果你想使用Cedet的工程管理功能,可以启用EDE Mode ----

(global-ede-mode t)

3 Cedet 的定制

3.1 基本Helpter的定制

Emacs 23.2中自带的Cedet中,去掉了原来的诸如 semantic-load-enable-minimum-features, semantic-load-enable-code-helpers 等等的命令,而是通过定制子模式(semantic-default-submodes)来确 定使用哪些辅助功能,最后使用semantic-mode 来启用这些功能。

以下是我的设置:

;;;;  Helper tools.

(custom-set-variables

 '(semantic-default-submodes (quote (global-semantic-decoration-mode global-semantic-idle-completions-mode

                                     global-semantic-idle-scheduler-mode global-semanticdb-minor-mode

                                     global-semantic-idle-summary-mode global-semantic-mru-bookmark-mode)))

 '(semantic-idle-scheduler-idle-time 3))



(semantic-mode)

3.2 Semantic/ia 的配置

semantic/ia 提供了机遇semantic的自动补齐,Tag信息显示等功能, Semantic/ia 可通过下面的代码来启用和优化

;; smart complitions

(require 'semantic/ia)

(setq-mode-local c-mode semanticdb-find-default-throttle

                 '(project unloaded system recursive))

(setq-mode-local c++-mode semanticdb-find-default-throttle

                 '(project unloaded system recursive))

3.3 头文件的设置

C/C++ 的开发中,和头文件要打很多交道。 头文件分为两种:系统头文件和用户自定义头文件。

3.3.1 系统头文件

如果我们使用的编译器为gcc,那么可以使用 semantic/gcc 来自动加载系统的头文件路径。

当然,在此基础上,我们也可以使用 semantic-add-system-include 来手动显示地添加某些路径。

3.3.2 用户头文件

用户自己定义的头文件,一般所在的位置可以用与当前路径的相对关系来表示出来, 然后在手动添加。

系统头文件和用户头文件的设置代码如下:

;;;; Include settings

(require 'semantic/bovine/gcc)

(require 'semantic/bovine/c)



(defconst cedet-user-include-dirs

  (list ".." "../include" "../inc" "../common" "../public" "."

        "../.." "../../include" "../../inc" "../../common" "../../public"))



(setq cedet-sys-include-dirs (list

                              "/usr/include"

                              "/usr/include/bits"

                              "/usr/include/glib-2.0"

                              "/usr/include/gnu"

                              "/usr/include/gtk-2.0"

                              "/usr/include/gtk-2.0/gdk-pixbuf"

                              "/usr/include/gtk-2.0/gtk"

                              "/usr/local/include"

                              "/usr/local/include"))



(let ((include-dirs cedet-user-include-dirs))

  (setq include-dirs (append include-dirs cedet-sys-include-dirs))

  (mapc (lambda (dir)

          (semantic-add-system-include dir 'c++-mode)

          (semantic-add-system-include dir 'c-mode))

        include-dirs))



(setq semantic-c-dependency-system-include-path "/usr/include/")

3.4 IMenu的集成

Semantic 可以通过imenu集成到Emacs菜单中, 从而通过菜单来显示和访问函数、变量以及其他Tag的列表。

Emacs 23.2 中,可以通过下面的代码来实现这个功能:

;;;; TAGS Menu

(defun my-semantic-hook ()

  (imenu-add-to-menubar "TAGS"))



(add-hook 'semantic-init-hooks 'my-semantic-hook)

3.5 Semanticdb 的定制

如果按照前面的代码中来定制HelperTool,sematicdb会自动启用。 这里我们首先需要自定义一下semanticdb的存放路径,例如我的:

;;;; Semantic DataBase存储位置

(setq semanticdb-default-save-directory

      (expand-file-name "~/.emacs.d/semanticdb"))

semanticdb 可以使用其他工具产生的Tag,例如GNU Global产生的Tags, 可以按照下面的代码来启用:

;; 使用 gnu global 的TAGS。

(require 'semantic/db-global)

(semanticdb-enable-gnu-global-databases 'c-mode)

(semanticdb-enable-gnu-global-databases 'c++-mode)

3.6 管理C/C++的工程

建议使用EDE来管理C/C++工程,下面的代码可用来定义一个工程:

(ede-cpp-root-project "Kernel"

                :name "Kernel Project"

                :file "~/Work/projects/kernel/linux-2.6.34/Makefile"

                :include-path '("/"

                                "/include"

                               )

                :system-include-path '("/usr/include")    )

这里有几个变量值得注意:

  • file:
    file可以为该程根目录下面的任意文件,该文件不用来解析,而只是这个工程的一个标志。
  • include-path:
    该变量是一个相对路径,指出了自定义的include目录。 其中的"/"并不表示系统的根目录,而表示该工程的根目录。
  • system-include-path:
    这是一个绝对路径,该路径指明了系统的Include目录。

4 Cedet的使用

4.1 命名补齐

这里的补齐包括函数名称,变量名等等,是很常用的一个功能。 个人以为最实用的一个补齐是 semantic-ia-complete-symbol, 他可以通过快捷键"C-c, /" 来调用。为了使用方便并和其他Package统一, 我将该函数添加到了hippie-expand中, 并将hippie-expand包进了自定义的函数indent-or-complete (从别人的配置文件中找到的)中,并将这个函数绑定到了Tab上。 这样,大多数情况下,通过Tab即可实现补齐或者对齐。 如果偶尔Tab不成功,再使用"M-/"或者"C-c, /"来修正一下。

这段配置的Lisp代码如下:

;;;;  缩进或者补齐

;;; hippie-try-expand settings

(setq hippie-expand-try-functions-list

      '(

        yas/hippie-try-expand

        semantic-ia-complete-symbol

        try-expand-dabbrev

        try-expand-dabbrev-visible

        try-expand-dabbrev-all-buffers

        try-expand-dabbrev-from-kill

        try-complete-file-name-partially

        try-complete-file-name

        try-expand-all-abbrevs))



(defun indent-or-complete ()

  "Complete if point is at end of a word, otherwise indent line."

  (interactive)

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

      (hippie-expand nil)

    (indent-for-tab-command)

    ))



(defun yyc/indent-key-setup ()

  "Set tab as key for indent-or-complete"

  (local-set-key  [(tab)] 'indent-or-complete)

  )

此外,对于C和C++的struct/class结构,函数semantic-complete-self-insert 可以插入类或结构中的成员变量,将至绑定到"."或者">",会加速代码编写的效率:

;;;; C-mode-hooks .

(defun yyc/c-mode-keys ()

  "description"

  ;; Semantic functions.

  (semantic-default-c-setup)

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

  (local-set-key "/C-cb" 'semantic-mrub-switch-tags)

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

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

  (local-set-key "/C-cp" 'semantic-ia-show-summary)

  (local-set-key "/C-cl" 'semantic-ia-show-doc)

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

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

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

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

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

  ;; Indent or complete

  (local-set-key  [(tab)] 'indent-or-complete)

  )

(add-hook 'c-mode-common-hook 'yyc/c-mode-keys)

4.2 获取Tag信息

semantic-ia-show-doc, semantic-ia-show-summary, semantic-ia-describe-class 这三个函数可以用来获取Tag信息,显示出代码的注释(包括Doxgen的注释), 对于阅读代码有很大帮助。

这些函数可以按照自己的习惯去绑定。其中前面两个的绑定过程在前面的 yyc/c-mode-keys中可以找到。

4.3 代码中的跳转

阅读代码时候,在不同的Tag中跳转是一个很有用的功能。 Cedet提供了这个功能,但是我手头上的Emacs 23.2 中自带的Cedet在Tag 跳转上有个问题————可以用 semantic-ia-fast-jump 跳转到Tag定义, 但是每次跳转后,却不能跳回来。

按照本文3.1节的方法设置Helper以后,在配置文件中添加下面的代码可以解决这一问题:

(defadvice push-mark (around semantic-mru-bookmark activate)

  "Push a mark at LOCATION with NOMSG and ACTIVATE passed to `push-mark'.

If `semantic-mru-bookmark-mode' is active, also push a tag onto

the mru bookmark stack."

  (semantic-mrub-push semantic-mru-bookmark-ring

                      (point)

                      'mark)

  ad-do-it)

4.4 查找函数调用

前面一节说的是在当前函数中,跳到Tag定义;而这里, 说的是查看当前光标下面的函数被哪些函数调用了。 Cedet中的 semantic-symref 实现了这一功能。 可以将该函数绑定到自己喜欢的快捷键上,如4.1中所示。

对于代码的浏览这个部分,我大部分时候使用的是global这个工具, global配置Xgtags来用,很方便。

4.5 Srecode的使用

Cedet提供了srecode,用于自动生成代码。但,个人感觉这个功能不怎么好用, 或者说是我不会用吧。

Emacs 23.2中自带的Cedet,仅提供了srecode的功能,但却没有将需要的template 一起和emacs一同发布出来。

对此,我的做法是修改了srecode-map-load-path,添加了 "~/.emacs.d/templates/srecode",

;;;; Custom template for srecode

(setq srecode-map-load-path

  (list (srecode-map-base-template-dir)

    (expand-file-name "~/.emacs.d/templates/srecode")

    ))

然后从cedet官网上 下载源码包,并把template解压到 "~/.emacs.d/templates/srecode"中。

然后将可以使用srecode-insert之类的函数来插入代码模版了。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值