Pinvon's Blog

所见, 所闻, 所思, 所想

Emacs C/C++ 配置

auto-complete或company

yasnippet

功能: 比如, 输入while()后, 按TAB键, 就会自动补充循环的内容. 在Sublime Text中很常用. 当然, 补充的内容还得自己改.

除了这些之外, 还能补充很多, 如常用的函数main(), cout等.

配置

(require 'yasnippet)
(yas-global-mode 1)

auto-complete-c-headers或company-c-headers

配置

查看C/C++头文件所在目录: gcc -xc++ -E -v -

记下 path/to/gcc/include

(defun my:ac-c-header-init ()
    (require 'auto-complete-c-headers)
    (add-to-list 'ac-sources 'ac-source-c-headers)
    (add-to-list 'achead:include-directories '"/usr/lib/gcc/x86_64-linux-gnu/5/include"))
(add-hook 'c++-mode-hook 'my:ac-c-header-init)
(add-hook 'c-mode-hook 'my:ac-c-header-init)

代码检查

安装flymake-google-cpplint

安装flymake-cursor

安装cpplint.py(静态代码检查工具)

pip install cpplint
which cpplint # 复制其路径

配置

(defun my:flymake-google-init ()
  (require 'flymake-google-cpplint)
  (custom-set-variables
   '(flymake-google-cpplint-command "/home/pinvon/.local/bin/cpplint"))
  (flymake-google-cpplint-load))
(add-hook 'c-mode-hook 'my:flymake-google-init)
(add-hook 'c++-mode-hook 'my:flymake-google-init)

google-c-style-mode

(require 'google-c-style)
(add-hook 'c-mode-common-hook 'google-set-c-style)
(add-hook 'c-mode-common-hook 'google-make-newline-indent)

cedet

semantic

功能

代码跳转, 自动补全, 分析源码

解释

插件一般是先load, 然后enable某个minor mode. semantic有很多mode, 一个一个去enable, 比较麻烦. 所以cedet定义了几个函数, 自动load并enable某些minor mode.

  • semantic-load-enable-minimum-features

    该函数开启三个特性:

    • semantic-idle-scheduler-mode: 这个模式让cedet在emacs空闲的时候自动分析buffer的内容.
    • semanticdb-minor-mode: 保存分析后的内容
    • semanticdb-load-ebrowse-caches
  • semantic-load-enable-code-helpers
    • semantic-load-enable-minimum-features
    • imenu: 显示semantic分析出的类和函数等
    • semantic-idle-summary-mode: 光标停留在类或函数上时, 显示原型
    • senator-minor-mode: 在emacs中增加senator菜单, 通过菜单在当前文件的各个tag之间移动跳转.
    • semantic-mru-bookmark-mode: 书签
  • semantic-load-enable-guady-code-helpers
    • semantic-load-enable-code-helpers
    • semantic-stickyfunc-mode: 显示当前函数名在buffer顶上
    • semantic-decoration-mode: 在类和函数上方添加蓝色的线
    • semantic-idle-completions-mode: 补全
  • semantic-load-enable-excessive-code-helpers
    • semantic-load-enable-gaudy-code-helpers
    • semantic-highlight-func-mode: 函数名高亮
    • semantic-idle-tag-highlight-mode: 光标停在某变量上, 使用到它的地方都变亮
    • semantic-decoration-on-*-members: private和protected的函数不同颜色标识
  • semantic-load-enable-semantic-debugging-helpers
    • semantic-highlight-edits-mode: 高亮最近修改
    • semantic-show-unmatched-syntax-mode: 红色下划线标记不匹配的语法
    • semantic-show-parser-state-mode

配置

(semantic-mode 1)
(defun my:add-semantic-to-autocomplete()
  (add-to-list 'ac-sources 'ac-source-semantic))
(add-hook 'c-mode-common-hook 'my:add-semantic-to-autocomplete)
(global-ede-mode 1)

(ede-cpp-root-project "my project" :file "~/demos/my_program/src/main.cpp"
    :include-path '("/../my_inc")
    :system-include-path '("/usr/include/c++/3.2.2/"))

其中, ede-cpp-root-project后面的my project可以自定义名字, :file是自己的项目根目录中任意一个文件, :include-path中的文件, 是其他的include文件, 但不是库文件.

Comments

使用 Disqus 评论
comments powered by Disqus