上面工作完成,就可以使用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 -bR,
-b 代表build 源碼的index 數據庫,-R 表示包括子文件夾,
cscope 在源代碼的根目錄生成index 文件cscope.out。
但是在默認情況下,cscope 在建庫時不會引入cpp 文件;如果是cpp 的項目,需要先找到所有的c/cpp 文件,然後再用cscope 建立index,命令如下:
- #找到所有的c,cpp,h 文件
- find . -name
"*.cpp" -o
-name "*.c"
-o -name
"*.h" > cscope.files
- #根據cscope.files 生成database
- 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 下所有文件
- @@ -138,7
+138,7 @@
- fi
- ) | \
- egrep -i '\.([chly](xx|pp)*|cc|hh)$'
| \
- - sed -e
'/\/CVS\//d' -e
'/\/RCS\//d' -e
's/^\.\///' | \
- + 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補全,看到所有命令。