emacs 中使用 cscope 閱讀代碼

安裝cscope:
(1)通過官網下載源碼
$tar zxfv cscope-15.5.tar.gz
$cd cscope-15.5
$./configure && make && make install
然後把contrib/xcscope/目錄下的cscope-indexer複製到PATH目錄比如/usr/local/bin,
再把xcscope.el複製到/usr/share/emacs/site-lisp,
修改/usr/share/emacs/site-lisp/site-start.el,添加(require 'xcscope)。
(2)直接通過工具安裝
如果是cygwin中選擇安裝包,安裝之後,xcscope.el被自動放置在/usr/share/emacs/site-lisp;
然後手工創建~/.emacs(沒有該文件則手工創建),加入(require 'xcscope),使用xcsope.el的配置。
如果是ubuntu的apt-get安裝,還要另外安裝cscope-el,不然找不到xcscope.el

sudo apt-get install cscope-el 

xcscope.el 在 /usr/share/emacs/site-lisp


上面工作完成,就可以使用cscope了,如果還想把xcscope.el文件放到系統用戶load-path下,可以試試下面方法:
先創建文件夾,mkdir -p ~/.emacs.d/site-lisp/
然後,在emacs的配置文件~/.emacs中添加:
;;LOAD_PATH      
(add-to-list 'load-path' "~/.emacs.d/site-lisp")
在.emacs文件中加入下面的語句:
(require 'xcscope)
如果只希望在打開c/c++文件的時候才加載xcscope,可以加入下面語句:
(add-hook 'c-mode-common-hook '(lambda() (require 'xcscope)))

修改cscope配置
xcscope默認的快捷鍵都是綁定到C-c s的前綴上面,如果你經常使用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)

cscope索引:
最簡單的方法是在源代碼目錄運行cscope -bR, 
-b 代表build 源碼的index 數據庫,-R 表示包括子文件夾,
cscope 在源代碼的根目錄生成index 文件cscope.out。
但是在默認情況下,cscope 在建庫時不會引入cpp 文件;如果是cpp 的項目,需要先找到所有的c/cpp 文件,然後再用cscope 建立index,命令如下:

點擊(此處)摺疊或打開

  1. #找到所有的c,cpp,h 文件
  2. find . -name "*.cpp" -o -name "*.c" -o -name "*.h" > cscope.files
  3. #根據cscope.files 生成database
  4. cscope -b -i cscope.files

不過這麼做太麻煩了,所以在cscope 的源代碼包中,在contrib/xcscope中有一個文件cscope-indexer,自動完成上述功能,搜索文件夾,並且建cscope 的index。 如果Linux發行版沒有把這個文件打到cscope的包中,可以下載源代碼包,然後把這個文件拷貝到/bin 下面,就可以用了。

利用cscope-indexer腳本生成文件列表和數據庫:
cscope-indexer -r
-r表示遞歸檢索子目錄,文件列表和數據庫的默認文件名分別爲cscope.files和cscope.out,可以使用-i,-f參數進行修改。

cscope-indexer可以根據自己的需要修改,例如這個網上的實例:用quilt 管理patch,會在源代碼目錄生成/.pc/目錄, 裏面是拷貝的c 文件,不希望cscope-indexer 去搜索這些文件,可以多加一個規則,慮除.pc 下所有文件

點擊(此處)摺疊或打開

  1. @@ -138,7 +138,7 @@
  2. fi
  3. ) | \
  4. egrep -i '\.([chly](xx|pp)*|cc|hh)$' | \
  5. - sed -e '/\/CVS\//d' -e '/\/RCS\//d' -e 's/^\.\///' | \
  6. + sed -e '/\/CVS\//d' -e '/\/RCS\//d' -e '/\/\.pc\//d' -e 's/^\.\///'| \
優化查找速度:
索引建好之後,如果發現查函數定義之類的操作很慢,可以進行一些優化:
(1)Cscope默認每次查找時更新cscope.out,可以關閉該選項以提高查找速度。
在~/.emacs文件中加入(setq cscope-do-not-update-database t)
(2)通過創建反向索引加速查找。
調用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。

上述工作完成之後,重啓emacs,就可以使用cscope了。

cscope的快捷方式:
(1)基本方式,參見xcscope.el的註釋部分:

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



(2)看代碼經常要跳轉,可以使用register來存儲標籤

C-x r SPC 編號  設置bookmark

C-x r j   編號  跳轉到bookmark

也可以通過完整的通用的命令輸入方式:
M-x point-to-register
M-x jump-to-register
輸入過程中可以用tab補全,看到所有命令。

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章