semantic功能介紹
1,代碼自動補全
3,代碼導航
啟動semantic功能:(semantic-mode 1)
1,Semantic mode 是輔助模式,當這個模式啟動了,你打開的每個緩沖區是否會被Semantic mode解析,取決於變量【semantic-new-buffer-setup-functions】,在被解析過的緩沖區里就可以使用semantic提供的函數來完成,代碼跳轉,查詢等。
2,當啟動Semantic mode 后,會自動啟動一些全局的輔助模式,可以用變量【semantic-default-submodes 】,來決定啟動哪些輔助模式。默認被啟動的全局的輔助模式是【SemanticDB mode】,【 Global Semantic Idle Scheduler mode 】;當然也可以通過執行函數來手動啟動這些輔助模式。在【Development】menu下,也可以找到這些輔助模式。
3,變量【semantic-new-buffer-setup-functions】是一個alist,它的默認值如下。也就是說當打開c,c++,java,js等后綴名的文件,semantic會自動解析這些緩沖區。當然可以添加或者刪除主編輯模式到這個alist里。
- c-mode
- c++-mode
- html-mode
- java-mode
- js-mode
- python-mode
4,變量【semantic-default-submodes】是一個alist,它的默認值是【SemanticDB mode】,【 Global Semantic Idle Scheduler mode 】。
下面的值也可以添加到semantic-default-submodes】里。
-
global-semanticdb-minor-mode’ - Maintain tag database.
-
global-semantic-idle-scheduler-mode’ - Reparse buffer when idle.
-
global-semantic-idle-summary-mode’ - Show summary of tag at point.
-
global-semantic-idle-completions-mode’ - Show completions when idle.
-
global-semantic-decoration-mode’ - Additional tag decorations.
-
global-semantic-highlight-func-mode’ - Highlight the current tag.
-
global-semantic-stickyfunc-mode’ - Show current fun in header line.
-
global-semantic-mru-bookmark-mode’ - Provide ‘switch-to-buffer’-like
keybinding for tag names.
-
global-cedet-m3-minor-mode’ - A mouse 3 context menu.
-
global-semantic-idle-local-symbol-highlight-mode’ - Highlight references
of the symbol under point.
-
global-semantic-highlight-edits-mode’ - Visualize incremental parser by
highlighting not-yet parsed changes.
-
global-semantic-show-unmatched-syntax-mode’ - Highlight unmatched lexical
syntax tokens.global-semantic-show-parser-state-mode’ - Display the parser cache state.
Idle Scheduler(global-semantic-idle-scheduler-mode )
這個子模式默認是啟動的,作用是:在用戶不操作emacs時,會自動重新解析所有需要被重新解析的buffer,如果在重新解析的過程中,有任何用戶的輸入了,就自動停止解析。
有幾個有用的變量:
1,semantic-idle-scheduler-idle-time :默認值是1秒,也就是說當用戶1秒沒有操作emacs后,就會自動重新解析這個buffer
2,semantic-idle-scheduler-max-buffer-size :可以被重新解析的buffer里面內容byte的最大值,如果超過這個值,就不去重新解析它。目的是,防止某個buffer過大,導致在重新解析它的時間過長,而影響了用戶使用emacs的體驗。如果是小於或者等於0的話,如果需要被重新解析的話,就無條件的解析。
3,semantic-idle-scheduler-no-working-message :解析時,是否顯示工作消息,默認值是nil,不顯示。
4,semantic-idle-scheduler-work-idle-time :當重新解析的同時,idle-scheduler還可以做如下的事情。做這些事特別耗時,所以當用戶60秒沒有操作emacs后,就會做下面的事。此變量的默認值是60秒。
-
Creating the include path caches required for symbol lookup.
創建 symbol lookup所需要的路徑緩存
-
Create data type caches.
創建數據類型的緩存
-
Saving SemanticDB caches to disk.
把緩存內容保存到本地磁盤
-
Speculatively parsing the files in the same directory as the current buffer.
解析和當前buffer在同一目錄下的所有文件
5,semantic-idle-work-parse-neighboring-files-flag :控制是否解析和當前buffer在同一目錄下的所有文件,默認值是nil,也就是不解析
6,semantic-before-idle-scheduler-reparse-hook :在idle scheduler 開始解析前,運行這個鈎子
7,semantic-after-idle-scheduler-reparse-hook :在idle scheduler 解析完成后,運行這個鈎子
Idle Summary Mode(global-semantic-idle-summary-mode )
啟動這個輔助mode后,當光標移到到某個函數調用的地方,或者變量使用的地方的時候,函數的原型(返回值,參數),變量的類型,會顯示在emacs最下面的輔助入力區。如下圖,當光標落在printf函數調用的地方時,會顯示處printf的原型
1,變量semantic-idle-summary-function 的作用:指定用哪種方式(哪個函數)來顯示函數的原型或者變量的定義等,它的默認值:semantic-format-tag-summarize-with-file。下面的函數付給semantic-format-tag-summarize-with-file。下面的函數存放在變量semantic-format-tag-functions 中。
- semantic-format-tag-name
- semantic-format-tag-canonical-name
- semantic-format-tag-abbreviate
- semantic-format-tag-summarize
- semantic-format-tag-summarize-with-file
- semantic-format-tag-short-doc
- semantic-format-tag-prototype
- semantic-format-tag-concise-prototype
- semantic-format-tag-uml-abbreviate
- semantic-format-tag-uml-prototype
- semantic-format-tag-uml-concise-prototype
- semantic-format-tag-prin1
2,變量semantic-idle-summary-out-of-context-faces 的作用:告訴Idle Summary Mode,當光標移動到哪些文本上的時候,不要嘗試去顯示它的摘要。比如當光標移動到注釋位置時,或者雙引號里面的字符串的時候,沒有必要讓Idle Summary Mode執行。這個變量是一個alist,可以添加或者刪除元素。它的默認值如下:
- font-lock-comment-face :注釋區域
- font-lock-string-face :雙引號里面的字符串區域
- font-lock-doc-string-face :注釋區域?
- font-lock-doc-face :注釋區域?
Idle Completions Mode(global-semantic-idle-completions-mode )
啟動這個輔助mode后,就可以有了代碼補齊的功能。但是需要注意:代碼補齊是在空間的時候才會生效,如果想立即生效請M-x semantic-complete-analyze-inline-idle 。
變量【semantic-complete-inline-analyzer-idle-displayor-class 】控制着補齊的樣式,補齊的樣式有3種:
- 第一種:Display completions in a tooltip (semantic-displayor-tooltip )
-
第二種:Display completions “inline” with the buffer text. This is the default value.(semantic-displayor-ghost)
從下圖可以看出來,當輸入n后,有4個補齊選項。當補齊選項顯示出來后,下面的命令就被激活了。
- 回車,ctrl-m:Accept the current completion (
semantic-complete-inline-done
), placing it in the buffer and moving point to the end of the completed tag - M-n :Select the next possible completion (
semantic-complete-inline-down
). The new completion is shown inline, replacing the old completion. - M-p :Select the previous possible completion (
semantic-complete-inline-up
). - TAB,ctrl-i:Accept as much of the completion as possible. If no additional completion can be accepted without ambiguity, select the next possible completion (
semantic-complete-inline-TAB
). - ctrl-g :Quit without completing (
semantic-complete-inline-quit
).
- 回車,ctrl-m:Accept the current completion (
- 第三種:Display completions in a separate window. (semantic-displayor-traditional )
Semantic Database(global-semanticdb-minor-mode )
啟動這個輔助mode后,Semantic 會把解析的結果存儲到硬盤上(路徑: ~/.emacs.d/semanticdb/ ),下次emacs再啟動后,還可以繼續使用上次的解析結果。
1,變量semanticdb-default-save-directory :控制Semantic Database的存儲路徑。
2,變量semanticdb-default-file-name :當變量semanticdb-default-save-directory的值是nil時,控制存儲解析結果的文件的名字(路徑是buffer所在的路徑)
3,變量semanticdb-persistent-path :指定哪些路徑下的解析結果可以被保存到磁盤。它是一個alist,里面放以字符串的形式放路徑。有個特殊的用法,它的值可以是never,always,project 。never:都不保存到磁盤;always:都保存到磁盤;project :變量【semanticdb-project-predicate-functions 】里指定的路徑被保存到磁盤。默認值是:always。
4,變量【semanticdb-project-predicate-functions 】:當變量【semanticdb-persistent-path】的值為project 時,它是一個alist,它里面放的路徑才會被保存到磁盤。默認值是nil。
Semanticdb Search Configuration
解析時,不只是解析當前buffer,還會自動解析包含的頭文件,比如c/c++的頭文件。在c/c++的編輯模式中,它會根據頭文件是【""】還是【<>】來區分是系統頭文件,還是自己項目里定義的頭文件。如果是系統頭文件,則去系統目錄去找。
變量【semanticdb-find-default-throttle 】決定了,怎么去搜索。默認是以時間為代價,去搜索到最大的准確性。
-
local:查找當前buffer所在目錄下的所有文件
-
project:查找自己指定的路徑。用變量【semanticdb-project-roots 】來控制具體的路徑信息。它是一個alist。假如使用了boost,則可以把boost的頭文件所在的路徑。
變量【semanticdb-project-root-functions 】是一個alist,里面放的是自己定義的函數,由一個參數,返回值必須是一個project-root或者nil,或者是a list of string( multiple project roots, for complex systems),函數的返回值覆蓋變量【semanticdb-project-roots 】。
用途:估計是根據參數,來動態決定【semanticdb-project-roots 】。
-
unloaded:如果要的解析結果沒加載到內存,則加載到內存,如果還沒解析,則馬上解析。
-
system:查找系統的頭文件。
- 函數semantic-add-system-include :添加路徑到系統路徑
- 函數semantic-remove-system-include :從系統路徑里,刪除某個路徑。
- 函數semantic-customize-system-include-path :讓當前buffer里的主mode,使用系統路徑。
-
recursive:遞歸查找頭文件里由include了別的頭文件。
變量【semanticdb-find-default-throttle 】的默認值:(local project unloaded system recursive)
Smart Jump
函數semantic-ia-fast-jump :跳到變量或者函數定義的地方
怎么跳回來呢??為了跳回去,要使用函數semantic-mrub-switch-tags(c-x B),但老是報出【Semantic Bookmark ring is currently empty】錯誤。原因是函數semantic-ia-fast-jump調用了函數push-mark,而函數push-mark里面沒有做push bookmark的操作,所以要修改函數push-mark,把bookmark放入semantic-mru-bookmark-ring里。放入后再執行semantic-mrub-switch-tags就不會出錯了。
注意:執行函數semantic-mrub-switch-tags時,可以指定要跳過去的tags,每次執行semantic-ia-fast-jump后,都把位置push到了變量semantic-mru-bookmark-ring里了,所以執行semantic-mrub-switch-tags時,可以指定這些被push進去的位置
(global-set-key [f12] 'semantic-ia-fast-jump)
(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)
;;跳轉后再跳回原來的地方
(global-set-key [f11]
(lambda()
(interactive)
(if (ring-empty-p (oref semantic-mru-bookmark-ring ring))
(error "Semantic Bookmark ring is currently empty"))
(let* ((ring (oref semantic-mru-bookmark-ring ring))
(alist (semantic-mrub-ring-to-assoc-list ring))
(first (cdr (car alist))))
(if (semantic-equivalent-tag-p (oref first tag)
(semantic-current-tag))
(setq first (cdr (car (cdr alist)))))
(semantic-mrub-switch-tags first))))
Smart Completion
1,函數semantic-complete-analyze-inline :也是自動補齊,那么和Idle Completions Mode里的自動補齊有什么區別呢?Idle Completions Mode是在空閑時間內執行的自動補齊,而這個是立即啟動自動補齊。綁定鍵:【C-c , 空格】。
補齊方式,也是由變量【semantic-complete-inline-analyzer-idle-displayor-class 】來控制。
Smart Summary
1,函數semantic-ia-show-summary :功能和Idle Summary Mode一樣,顯示函數,變量的信息。
2,函數semantic-ia-show-doc :顯示函數,變量的注釋信息。
3,函數semantic-ia-describe-class :顯示結構體,類里面的成員信息。
4,當發生不能解析的情況,參考Debugging the Semantic Analyzer
speedbar
讓speedbar使用由semantic解析出來的tags,semantic解析出來的tags里的信息比speedbar自己的tags要詳細
使用semantic后speedbar的效果如下,比不用semantic多出了【variables】【functions】【misc】,詳細了好多哦。
Sticky Function mode
啟動(M-x global-semantic-stickyfunc-mode)這個模式后,在emacs最上面一行顯示如下圖:
c/c++ 學習互助QQ群:877684253
本人微信:xiaoshitou5854
2085156153.png)