您的位置:首页 > 运维架构

cscope 在emacs里的使用

2011-06-07 19:45 363 查看



在windows
下通常使用source insight

读源代码,本人希望将这项工作转移到linux
下面。在配置并试用vim
一段时间后,感觉不是特别满意。幸运的是,这个挑剔的人并不懒惰,开始尝试使用强大的emacs
。在这里,并不介绍emacs
的基本操作以及
配置,建议学习emacs
自带的帮助或者阅读《学习GNU
Emacs
》,而是与大家分享cscope
在emacs
中的配置与使用。

作为菜鸟在
开源世界旅行的第一站,即本人的第一篇linux
学习笔记,决定向大牛们学习,在正文开始之前注明
文章版权和参考文献,毕竟水文也可以有版权:)
希望本文对您有所帮助,也欢迎您给我提意见和建
议。我的邮箱是:intrepyd@gmail.com




版权说
明和参考文献

1.
版权说明

转载请注明
转自潘云登的专栏 ,请保证全文转载,尤其不可省略这一部分。

2.
参考文献

cscope-indexer
脚本和xcscope.el

件的注释部分


Cscope



简介


对于浏览源
代码来说,在 Emacs
里面也有很多工具可用,比如自带的 etags
就不错,不过功能不如 Cscope

大。它最初是由 Bell
实验室开发,并且多年以来一直是
AT&T Unix
商业发行版的一部分。它已经被用于管理超过两千万行代码的工程。在2000
年4
月,多亏了 Santa Cruz Operation,
Inc. (SCO)
, Cscope
的源代码在 BSD license
下开放了源代码Blah~Blah~

使用Cscope
,主要用来回答以下问题:

Where
is this variable used?

What
is the value of this preprocessor symbol?

Where
is this function in the source files?

What
functions call this function?

What
functions are called by this function?

Where
does the message "out of space" come from?

Where
is this source file in the directory structure?

What
files include this header file?


安装和
配置

1.
首先,在系统中安装Cscope
。Linux
下,安装软件的方式通常有两种:

方式一:下
Cscope


源代码,按照源码包中的INSTALL
说明文件,执行下面的命令进行配置编译安装

./configure

make

make
install

Cscope
的源码包里面有个xcscope.el

件,为(X)Emacs
提供了Cscope

口。它处于源码包的 contrib/xcscope
目录下。该目录下面还有一个名为cscope-indexer
的脚本文件,用于创建文件列表和数据库。

方式二:利
用linux
发行版的软件管理工具进行安装。我使用的是Jaunty
Jackalope
(Ubuntu 9.04
),只要一条命令,解决所有问题。

sudo
apt-get
install cscope

安装后,cscope-indexer

本位于/usr/bin
目录下,xcscope.el

置于emacs
默认的load-path
下,
在我的系统中是/usr/share/emacs/site-lisp


2.
为了能够执行cscope-indexer

本,需要将它放到PATH
变量指向的文件夹下,如/usr/bin

并确认该脚本具有执行权限。

3.
把xcscope.el

件放到系统用户的load-path
下。为系统用户创建文件夹并将其加入load-path
的方法是:先创建文件夹,

mkdir
-p
~/.emacs.d/site-lisp/

然后,在emacs

配置文件~/.emacs
中添加

;;LOAD_PATH

(add-to-list

'load-path' "~/.emacs.d/site-lisp")

如果没有该文件,请手动创建。

4.
在.emacs

件中加入下面的语句

(require

'xcscope)

或者,你希望只在打开c/c++

件的时候才加载xcscope
,可以加入

(add-hook

'c-mode-common-hook '(lambda() (require 'xcscope)))

5.xcscope
默认的快捷键都是绑定到C-c
s
的前缀上面,如果你经常使用xcscope.el
,可以自己进行按键绑定,减少
击键次数。不要担心别人笑你懒,xcscope.el
的编写者就鼓励我们这样做:-)
。具体方法是,在.emacs
文件中加入

(define-key

global-map [(control f3)]
'cscope-set-initial-directory)

(define-key

global-map [(control f4)]
'cscope-unset-initial-directory)

(define-key

global-map [(control f5)]
'cscope-find-this-symbol)

(define-key

global-map [(control f6)]
'cscope-find-global-definition)

(define-key

global-map [(control f7)]
'cscope-find-global-definition-no-prompting)

(define-key

global-map [(control f8)]
'cscope-pop-mark)

(define-key

global-map [(control f9)]
'cscope-next-symbol)

(define-key

global-map [(control f10)] 'cscope-next-file)

(define-key

global-map [(control f11)] 'cscope-prev-symbol)

(define-key

global-map [(control f12)] 'cscope-prev-file)

(define-key

global-map [(meta f9)]
'cscope-display-buffer)

(define-key

global-map [(meta f10)]
'cscope-display-buffer-toggle)

6.
重启emacs

使配置生效。


基本使


这里以内核源码为例,介绍Cscope

基本用法。

1.
首先,在源码根目录下,如~/kernerl/linux-2.6.29.3,

用cscope-indexer
脚本生成文件列表和数据库,方法是执行

cscope-indexer

-r

-r
参数表示递归检索子目录,文件列表和数据库的默认文件名分别为cscope.files
和cscope.out
,可
以使用-i,-f
参数进行修改,请参考man

解脚本参数用法。

2.
激动人心的时刻到了。用emacs

开init/main.c
,C-s
搜索sched_init
函数,将光标停在函数名上,按C-c s d

者先前设置的Ctrl+F6,
回车进行查找。结果居然用了35.32
秒,
汗!原来,Cscope
默认在每次进行查找时更新cscope.out

当工程十分庞大时,建议关闭该选项以提高查找速度。方法是在~/.emacs
文件中加入

(setq

cscope-do-not-update-database t)

重复上述操作,结果仍然用了9.89
秒,再汗!莫非是我的古董本太慢?

3.
百度一下,你就知道:)

Cscope
可以通过创建反向索引加速查找,方法是调用Cscope
时,使用-q
参数。真的假的,一试便知。修改cscope-indexer

本,将

cscope
-b -i
$LIST_FILE -f $DATABASE_FILE

替换为

cscope
-q -b -i
$LIST_FILE -f $DATABASE_FILE

进入内核根目录,删除先前的文件
列表和数据库,重新调用cscope-indexer
。这回多生成了两个文件,cscope.in.out
和cscope.po.out

重试刚才的查找,结果只用了0.08
秒,
大功告成。

4.
贴张结果,庆祝一下:)







附:默
认的按键绑定

;;
* Keybindings:

;;

;;
All
keybindings use the "C-c s" prefix, but are usable only while

;;
editing a
source file, or in the cscope results buffer:

;;

;;

C-c s s
Find symbol.

;;

C-c s d
Find global definition.

;;

C-c s g
Find global definition
(alternate binding).

;;

C-c s G
Find global definition
without prompting.

;;

C-c s c
Find functions calling a
function.

;;

C-c s C
Find called functions
(list functions called

;;

from a function).

;;

C-c s t
Find text string.

;;

C-c s e
Find egrep pattern.

;;

C-c s f
Find a file.

;;

C-c s i
Find files #including a
file.

;;

;;
These pertain
to navigation through the search results:

;;

;;

C-c s b
Display *cscope* buffer.

;;

C-c s B
Auto display *cscope*
buffer toggle.

;;

C-c s n
Next symbol.

;;

C-c s N
Next file.

;;

C-c s p
Previous symbol.

;;

C-c s P
Previous file.

;;

C-c s u
Pop mark.

;;

;;
These pertain
to setting and unsetting the variable,

;;

`cscope-initial-directory', (location searched for the cscope database

;;

directory):

;;

;;

C-c s a
Set initial directory.

;;

C-c s A
Unset initial directory.

;;

;;
These pertain
to cscope database maintenance:

;;

;;

C-c s L
Create list of files to
index.

;;

C-c s I
Create list and index.

;;

C-c s E
Edit list of files to
index.

;;

C-c s W
Locate this buffer's
cscope directory

;;

("W" --> "where").

;;

C-c s S
Locate this buffer's
cscope directory.

;;

(alternate binding: "S" --> "show").

;;

C-c s T
Locate this buffer's
cscope directory.

;;

(alternate binding: "T" --> "tell").

;;

C-c s D

Dired
this buffer's directory.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: