emacs下cedet的配置

2023-10-29 22:58
文章标签 配置 emacs cedet

本文主要是介绍emacs下cedet的配置,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

主要参考http://blog.163.com/vic_kk/blog/static/494705242010726297405#sec-4_3

其他可以参考http://emacser.com/cedet.htm

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-modeglobal-semantic-idle-scheduler-mode global-semanticdb-minor-modeglobal-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代码如下(但是我没有使用这段,因为我用的是auto-complete):

;;;;  缩进或者补齐
;;; hippie-try-expand settings
(setq hippie-expand-try-functions-list'(yas/hippie-try-expandsemantic-ia-complete-symboltry-expand-dabbrevtry-expand-dabbrev-visibletry-expand-dabbrev-all-bufferstry-expand-dabbrev-from-killtry-complete-file-name-partiallytry-complete-file-nametry-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之类的函数来插入代码模版了。




这篇关于emacs下cedet的配置的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/303930

相关文章

springboot3.x使用@NacosValue无法获取配置信息的解决过程

《springboot3.x使用@NacosValue无法获取配置信息的解决过程》在SpringBoot3.x中升级Nacos依赖后,使用@NacosValue无法动态获取配置,通过引入SpringC... 目录一、python问题描述二、解决方案总结一、问题描述springboot从2android.x

nginx跨域访问配置的几种方法实现

《nginx跨域访问配置的几种方法实现》本文详细介绍了Nginx跨域配置方法,包括基本配置、只允许指定域名、携带Cookie的跨域、动态设置允许的Origin、支持不同路径的跨域控制、静态资源跨域以及... 目录一、基本跨域配置二、只允许指定域名跨域三、完整示例四、配置后重载 nginx五、注意事项六、支持

Spring配置扩展之JavaConfig的使用小结

《Spring配置扩展之JavaConfig的使用小结》JavaConfig是Spring框架中基于纯Java代码的配置方式,用于替代传统的XML配置,通过注解(如@Bean)定义Spring容器的组... 目录JavaConfig 的概念什么是JavaConfig?为什么使用 JavaConfig?Jav

Spring Boot Interceptor的原理、配置、顺序控制及与Filter的关键区别对比分析

《SpringBootInterceptor的原理、配置、顺序控制及与Filter的关键区别对比分析》本文主要介绍了SpringBoot中的拦截器(Interceptor)及其与过滤器(Filt... 目录前言一、核心功能二、拦截器的实现2.1 定义自定义拦截器2.2 注册拦截器三、多拦截器的执行顺序四、过

springboot的controller中如何获取applicatim.yml的配置值

《springboot的controller中如何获取applicatim.yml的配置值》本文介绍了在SpringBoot的Controller中获取application.yml配置值的四种方式,... 目录1. 使用@Value注解(最常用)application.yml 配置Controller 中

springboot中配置logback-spring.xml的方法

《springboot中配置logback-spring.xml的方法》文章介绍了如何在SpringBoot项目中配置logback-spring.xml文件来进行日志管理,包括如何定义日志输出方式、... 目录一、在src/main/resources目录下,也就是在classpath路径下创建logba

C++多线程开发环境配置方法

《C++多线程开发环境配置方法》文章详细介绍了如何在Windows上安装MinGW-w64和VSCode,并配置环境变量和编译任务,使用VSCode创建一个C++多线程测试项目,并通过配置tasks.... 目录下载安装 MinGW-w64下载安装VS code创建测试项目配置编译任务创建 tasks.js

Nginx概念、架构、配置与虚拟主机实战操作指南

《Nginx概念、架构、配置与虚拟主机实战操作指南》Nginx是一个高性能的HTTP服务器、反向代理服务器、负载均衡器和IMAP/POP3/SMTP代理服务器,它支持高并发连接,资源占用低,功能全面且... 目录Nginx 深度解析:概念、架构、配置与虚拟主机实战一、Nginx 的概念二、Nginx 的特点

2025最新版Android Studio安装及组件配置教程(SDK、JDK、Gradle)

《2025最新版AndroidStudio安装及组件配置教程(SDK、JDK、Gradle)》:本文主要介绍2025最新版AndroidStudio安装及组件配置(SDK、JDK、Gradle... 目录原生 android 简介Android Studio必备组件一、Android Studio安装二、A

前端Visual Studio Code安装配置教程之下载、汉化、常用组件及基本操作

《前端VisualStudioCode安装配置教程之下载、汉化、常用组件及基本操作》VisualStudioCode是微软推出的一个强大的代码编辑器,功能强大,操作简单便捷,还有着良好的用户界面,... 目录一、Visual Studio Code下载二、汉化三、常用组件1、Auto Rename Tag2