海上月明

editer by sun
posts - 162, comments - 51, trackbacks - 0, articles - 8
   :: 首页 :: 新随笔 ::  :: 聚合  :: 管理

[转]emacs 代码自动补齐的三个方法

Posted on 2008-10-16 17:29 pts 阅读(5413) 评论(0)  编辑  收藏 所属分类: Emacs
Emacs -- 自动补齐

在编写代码时,自动补齐(成员函数变量,以及……)能提高很大的效率,emacs的自动补齐方法有很多种,我参考了很多其他网友的文章,简单总结了下,希望其他网友不要怪罪我哈,呵呵,我希望把我的学习过程记录下来,能对其他网友有所帮助.以下是几种不同的方法(也可以一块用哈)
1. Emacs 自带的hippie-expand (参考的是王垠的)
hippie-expand是 Emacs 自带的功能,
把M-/ 绑定到 hippie-expand,在.emacs文件中加入
;;绑定按键
(global-set-key [(meta ?/)] 'hippie-expand)

hippie-expand 的补全方式。它是一个优先列表, hippie-expand 会优先使用表最前面的函数来补全。通常的设置是:

(setq hippie-expand-try-functions-list
'(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
try-expand-list
try-expand-line
try-complete-lisp-symbol-partially
try-complete-lisp-symbol))
首先使用当前的buffer补全,如果找不到,就到别的可见的窗口里寻找,
如果还找不到,那么到所有打开的buffer去找,如果还……那么到kill-ring里,
到文件名,到简称列表里,到list,…… 当前使用的匹配方式会在 echo 区域
显示.
确实是非常好用,基本上我M-/就能到达我想要的了.

2 采用etags
etags能像cscope那样,在代码里跳来跳去,比如查找函数,变量等,它还能够自动补齐代码.
1),先生成etags文件
find . /usr/include/ -name "*.c" -or -name "*.cpp" -or -name "*.hpp" -or -name "*.h" |xargs etags --members --language=c++
2).配置.emacs
(setq tags-file-name "~/TAGS")
3),使用
在emacs中,M-tab 就可以自动补齐了,不过有时候还是不是很好用.
M-. 查找一个tag,比如函数定义类型定义等。
C-u M-. 查找下一个tag的位置
M-* 回到上一次运行M-.前的光标位置。 M-TAB 自动补齐函数名。

3 采用cedet包
1)下载cedet
网址是 http://cedet.sourceforge.net/
2)编译
tar -zxf cedet-1.0pre3.tar.gz
cd cedet-1.0pre3
make
如果make不成功的话,就看看那个说明吧
3)配置
查看emacs的配置文件在哪里
whereis emacs
拷贝编译好了的cedet
cp -r cedet-1.0pre3 /usr/share/emacs/
查看是否有我们需要的那个文件
ls /usr/share/emacs/cedet-1.0pre3/common/cedet.el

配置.emacs文件,在.emacs文件中加入
;;;;;;;;;;cedet
(load-file "/usr/share/emacs/cedet-1.0pre3/common/cedet.el")

;;设置检索范围
(setq semanticdb-project-roots
(list
(expand-file-name "/")));;可以设置为项目的顶级目录

;;绑定按键,ctr+tab,以下三种,任意选择一个,我喜欢第二个
;;(global-set-key [(control tab)] 'senator-complete-symbol);
(global-set-key [(control tab)] ' senator-completion-menu-popup)
;; (global-set-key [(control tab)] 'semantic-ia-complete-symbol-menu)

4)使用
在一个未输入完成的函数上尝试下ctr+tab键

只有注册用户登录后才能发表评论。


网站导航: