2015-06-10 16:44:30 +00:00
#+TITLE : C/C++ contribution layer for Spacemacs
[[file:img/ccpp.jpg ]]
[[file:img/cmake.png ]]
* Table of Contents :TOC@4:
- [[#description ][Description ]]
- [[#features ][Features ]]
- [[#install ][Install ]]
- [[#layer ][Layer ]]
- [[#default-mode-for-header-files ][Default mode for header files ]]
- [[#enable-clang-support ][Enable Clang support ]]
- [[#clang-format ][clang-format ]]
- [[#company-clang-and-flycheck ][Company-clang and flycheck ]]
- [[#key-bindings ][Key Bindings ]]
* Description
This layer adds configuration for C/C++ language as well support for [[http:/ /www.cmake.org/ ][CMake]]
scripts.
* Features
- Support syntax checking via flycheck with Clang.
- Support code reformatting with [[http://clang.llvm.org/docs/ClangFormat.html ][clang-format ]].
- Display function or variable definition at the bottom. (when =semantic= layer is included)
2015-06-10 21:16:01 +00:00
- Display current function cursor is in at the top. See [[https://github.com/tuhdo/semantic-stickyfunc-enhance ][stickyfunc-demos ]] for
demos in some programming languages. (when =semantic= layer is included)
- Support common refactoring with [[https://github.com/tuhdo/semantic-refactor ][semantic-refactor ]] . See [[https://github.com/tuhdo/semantic-refactor/blob/master/srefactor-demos/demos.org ][srefactor-demos ]] for
demonstration of refactoring features. (when =semantic= layer is included)
2015-06-10 16:44:30 +00:00
* Install
** Layer
To use this contribution add it to your =~/.spacemacs=
#+BEGIN_SRC emacs-lisp
(setq-default dotspacemacs-configuration-layers '(c-c++))
#+END_SRC
2015-06-10 21:16:01 +00:00
*Note:* [[https://github.com/tuhdo/semantic-refactor ][semantic-refactor ]] is only available for Emacs 24.4+
2015-06-10 16:44:30 +00:00
** Default mode for header files
2015-06-10 21:16:01 +00:00
By default header files are opened in =c-mode= , you can open them in =c++-mode=
2015-06-10 16:44:30 +00:00
by setting the variable =c-c++-default-mode-for-headers= to =c++-mode= .
#+BEGIN_SRC emacs-lisp
(setq-default dotspacemacs-configuration-layers
2015-06-26 05:15:00 +00:00
'((c-c++ :variables
2015-06-10 16:44:30 +00:00
c-c++-default-mode-for-headers 'c+ +-mode)))
#+END_SRC
2015-06-10 21:16:01 +00:00
*Note:* To set the variable for a given project, create a directory local
2015-06-10 16:44:30 +00:00
variable at the root of your project. More info on directory local variables
can be found in the [[http://www.gnu.org/software/emacs/manual/html_node/elisp/Directory-Local-Variables.html ][dir-locals ]].
** Enable Clang support
To enable Clang support set the layer variable =c-c++-enable-clang-support=
to =t= in the dotfile:
#+BEGIN_SRC emacs-lisp
(setq-default dotspacemacs-configuration-layers
'((c-c++ :variables c-c+ +-enable-clang-support t)))
#+END_SRC
*** clang-format
[[http://clang.llvm.org/docs/ClangFormat.html ][clang-format ]] allows reformatting either a selected region of code
(=clang-format-region= ) or a whole buffer (=clang-format-buffer= ) to make it
conform to a style defined in a =.clang-format= file. This file is either
located in the same directory as the file being edited, or in any of its parent
directories (otherwise a default style will be used).
You can add snippets similar to the following to bind clang-format to either a
particular mode or all modes in your =dotspacemacs/config= (within your
=~/.spacemacs= ):
#+BEGIN_SRC emacs-lisp
;; Bind clang-format-region to C-M-tab in all modes:
(global-set-key [C-M-tab] 'clang-format-region)
;; Bind clang-format-buffer to tab on the c++-mode only:
(add-hook 'c++-mode-hook 'clang-format-bindings)
(defun clang-format-bindings ()
(define-key c++-mode-map [tab] 'clang-format-buffer))
#+END_SRC
*** Company-clang and flycheck
This layer adds some fancy improvements to =company-clang= .
It includes a hook to load a projects =.clang_complete= file, which is
just a text file with one clang flag per line, a format also used by
other text editor clang plugins.
Not only does this allow proper autocomplete on projects with extra
includes and flags, but there is also support for flycheck so that it
doesn't complain about missing header files.
* Key Bindings
| Key Binding | Description |
|-------------+------------------------------------------------------------------------|
| ~SPC m g o~ | open matching file (e.g. switch between .cpp and .h) |
| ~SPC m g O~ | open matching file in another window (e.g. switch between .cpp and .h) |
| ~SPC m r~ | srefactor: refactor thing at point. |
2015-06-10 21:16:01 +00:00
*Note:* [[https://github.com/tuhdo/semantic-refactor ][semantic-refactor ]] is only available for Emacs 24.4+