cscope 在emacs里的使用

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

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

 


版权说 明和参考文献

1. 版权说明

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

2. 参考文献

cscope-indexer 脚本和xcscope.el 文 件的注释部分

 


Cscope

简介

对于浏览源 代码来说,在 Emacs 里面也有很多工具可用,比如自带的 etags 就不错,不过功能不如 Cscope 强 大。它最初是由 Bell 实验室开发,并且多年以来一直是 AT&T Unix 商业发行版的一部分。它已经被用于管理超过两千万行代码的工程。在20004 月,多亏了 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. 首先,在系统中安装CscopeLinux 下,安装软件的方式通常有两种:

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

./configure

make

make install

Cscope 的源码包里面有个xcscope.el 文 件,为(X)Emacs 提供了Cscope 接 口。它处于源码包的 contrib/xcscope 目录下。该目录下面还有一个名为cscope-indexer 的脚本文件,用于创建文件列表和数据库。

方式二:利 用linux 发行版的软件管理工具进行安装。我使用的是Jaunty JackalopeUbuntu 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.filescscope.out ,可 以使用-i,-f 参数进行修改,请参考man 了 解脚本参数用法。

 

2. 激动人心的时刻到了。用emacs 打 开init/main.cC-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.outcscope.po.out 。 重试刚才的查找,结果只用了0.08 秒, 大功告成。

 

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

 

cscope查找函数定义

 


附:默 认的按键绑定

;; * 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.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值