4 Cedet的使用
4.1 命名補齊
這里的補齊包括函數(shù)名稱,變量名等等,是很常用的一個功能。 個人以為最實用的一個補齊是 semantic-ia-complete-symbol, 他可以通過快捷鍵"C-c, /" 來調(diào)用。為了使用方便并和其他Package統(tǒng)一, 我將該函數(shù)添加到了hippie-expand中, 并將hippie-expand包進了自定義的函數(shù)indent-or-complete (從別人的配置文件中找到的)中,并將這個函數(shù)綁定到了Tab上。 這樣,大多數(shù)情況下,通過Tab即可實現(xiàn)補齊或者對齊。 如果偶爾Tab不成功,再使用"M-/"或者"C-c, /"來修正一下。
這段配置的Lisp代碼如下:
;;;; 縮進或者補齊
;;; hippie-try-expand settings
(setq hippie-expand-try-functions-list
'(
yas/hippie-try-expand
semantic-ia-complete-symbol
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))
(defun indent-or-complete ()
"Complete if point is at end of a word, otherwise indent line."
(interactive)
(if (looking-at "\\>")
(hippie-expand nil)
(indent-for-tab-command)
))
(defun yyc/indent-key-setup ()
"Set tab as key for indent-or-complete"
(local-set-key [(tab)] 'indent-or-complete)
)
此外,對于C和C++的struct/class結(jié)構(gòu),函數(shù)semantic-complete-self-insert 可以插入類或結(jié)構(gòu)中的成員變量,將至綁定到"."或者">",會加速代碼編寫的效率:
;;;; C-mode-hooks .
(defun yyc/c-mode-keys ()
"description"
;; Semantic functions.
(semantic-default-c-setup)
(local-set-key "\C-c?" 'semantic-ia-complete-symbol-menu)
(local-set-key "\C-cb" 'semantic-mrub-switch-tags)
(local-set-key "\C-cR" 'semantic-symref)
(local-set-key "\C-cj" 'semantic-ia-fast-jump)
(local-set-key "\C-cp" 'semantic-ia-show-summary)
(local-set-key "\C-cl" 'semantic-ia-show-doc)
(local-set-key "\C-cr" 'semantic-symref-symbol)
(local-set-key "\C-c/" 'semantic-ia-complete-symbol)
(local-set-key [(control return)] 'semantic-ia-complete-symbol)
(local-set-key "." 'semantic-complete-self-insert)
(local-set-key ">" 'semantic-complete-self-insert)
;; Indent or complete
(local-set-key [(tab)] 'indent-or-complete)
)
(add-hook 'c-mode-common-hook 'yyc/c-mode-keys)
4.2 獲取Tag信息
semantic-ia-show-doc, semantic-ia-show-summary, semantic-ia-describe-class 這三個函數(shù)可以用來獲取Tag信息,顯示出代碼的注釋(包括Doxgen的注釋), 對于閱讀代碼有很大幫助。
這些函數(shù)可以按照自己的習(xí)慣去綁定。其中前面兩個的綁定過程在前面的 yyc/c-mode-keys中可以找到。
4.3 代碼中的跳轉(zhuǎn)
閱讀代碼時候,在不同的Tag中跳轉(zhuǎn)是一個很有用的功能。 Cedet提供了這個功能,但是我手頭上的Emacs 23.2 中自帶的Cedet在Tag 跳轉(zhuǎn)上有個問題————可以用 semantic-ia-fast-jump 跳轉(zhuǎn)到Tag定義, 但是每次跳轉(zhuǎn)后,卻不能跳回來。
按照本文3.1節(jié)的方法設(shè)置Helper以后,在配置文件中添加下面的代碼可以解決這一問題:
(defadvice push-mark (around semantic-mru-bookmark activate)
"Push a mark at LOCATION with NOMSG and ACTIVATE passed to `push-mark'.
If `semantic-mru-bookmark-mode' is active, also push a tag onto
the mru bookmark stack."
(semantic-mrub-push semantic-mru-bookmark-ring
(point)
'mark)
ad-do-it)
4.4 查找函數(shù)調(diào)用
前面一節(jié)說的是在當前函數(shù)中,跳到Tag定義;而這里, 說的是查看當前光標下面的函數(shù)被哪些函數(shù)調(diào)用了。 Cedet中的 semantic-symref 實現(xiàn)了這一功能。 可以將該函數(shù)綁定到自己喜歡的快捷鍵上,如4.1中所示。
對于代碼的瀏覽這個部分,我大部分時候使用的是global這個工具, global配置Xgtags來用,很方便。
4.5 Srecode的使用
Cedet提供了srecode,用于自動生成代碼。但,個人感覺這個功能不怎么好用, 或者說是我不會用吧。
Emacs 23.2中自帶的Cedet,僅提供了srecode的功能,但卻沒有將需要的template 一起和emacs一同發(fā)布出來。
對此,我的做法是修改了srecode-map-load-path,添加了 "~/.emacs.d/templates/srecode",
;;;; Custom template for srecode
(setq srecode-map-load-path
(list (srecode-map-base-template-dir)
(expand-file-name "~/.emacs.d/templates/srecode")
))
然后從cedet官網(wǎng)上 下載源碼包,并把template解壓到 "~/.emacs.d/templates/srecode"中。
然后將可以使用srecode-insert之類的函數(shù)來插入代碼模版了。
本文導(dǎo)航
- 第1頁: 首頁
- 第2頁: Cedet 的定制
- 第3頁: Cedet的使用