spacemacs/layers/+lang/c-c++
syl20bnr 74fdbb6795 Refactor and simplify company backends declaration
Enabling a company backend for a specific mode was a tedious tasks with code
scattered at different locations, one for local variable definitions, one for
company hook function definitions and another where the backends were pushed to
the local variables (which was problematic, since we ended up pushing the same
backends over and over again with `SPC f e R`, pushes have been replaced by
add-to-list calls in the new macro).

All these steps are now put together at one place with the new macro
spacemacs|add-company-backends, check its docstring for more info on its
arguments.

This macro also allows to define arbitrary buffer local variables to tune
company for specific modes (similar to layer variables via a keyword :variables)

The code related to company backends management has been moved to the
auto-completion layer in the funcs.el file. A nice side effect of this move is
that it enforces correct encapsulation of company backends related code. We can
now easily detect if there is some configuration leakage when the
auto-completion layer is not used. But we loose macro expansion at file loading
time (not sue it is a big concern though).

The function spacemacs|enable-auto-complete was never used so it has been
deleted which led to the deletion of the now empty file core-auto-completion.el.

The example in LAYERS.org regarding auto-completion is now out of date and has
been deleted. An example to setup auto-completion is provided in the README.org
file of the auto-completion layer.
2017-01-02 00:39:04 -05:00
..
img
config.el Refactor and simplify company backends declaration 2017-01-02 00:39:04 -05:00
funcs.el Refactor and simplify company backends declaration 2017-01-02 00:39:04 -05:00
packages.el Refactor and simplify company backends declaration 2017-01-02 00:39:04 -05:00
README.org Convert org doc files with doc-fmt 2016-03-30 22:59:55 -04:00

C/C++ layer

/TakeV/spacemacs/media/commit/c22991d587d769e584cad398bd8a517d221e86af/layers/+lang/c-c++/img/ccpp.jpg /TakeV/spacemacs/media/commit/c22991d587d769e584cad398bd8a517d221e86af/layers/+lang/c-c++/img/cmake.png

Description

This layer adds configuration for C/C++ language as well support for CMake scripts.

Features

  • Support syntax checking via flycheck with Clang.
  • Support for disassembly of code with disaster.
  • Support code reformatting with clang-format.
  • Display function or variable definition at the bottom. (when semantic layer is included)
  • Display current function cursor is in at the top. See stickyfunc-demos for demos in some programming languages. (when semantic layer is included)
  • Support common refactoring with semantic-refactor . See srefactor-demos for demonstration of refactoring features. (when semantic layer is included)
  • Support code navigation via cscope (when cscope layer is included) and gtags.
  • Support auto-completion (when auto-completion layer is included) via company-clang (when c-c++-enable-clang-support is turned on), or company-ycmd (when ycmd layer is included).

Install

Layer

To use this configuration layer, add it to your ~/.spacemacs. You will need to add c-c++ to the existing dotspacemacs-configuration-layers list in this file.

Note: semantic-refactor is only available for Emacs 24.4+

Default mode for header files

By default header files are opened in c-mode, you can open them in c++-mode by setting the variable c-c++-default-mode-for-headers to c++-mode.

  (setq-default dotspacemacs-configuration-layers
    '((c-c++ :variables
             c-c++-default-mode-for-headers 'c++-mode)))

Note: To set the variable for a given project, create a directory local variable at the root of your project. More info on directory local variables can be found in the dir-locals.

Enable Clang support

To enable Clang support set the layer variable c-c++-enable-clang-support to t in the dotfile:

  (setq-default dotspacemacs-configuration-layers
    '((c-c++ :variables c-c++-enable-clang-support t)))

clang-format

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/user-config (within your ~/.spacemacs):

  ;; 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))

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 a open matching file (e.g. switch between .cpp and .h)
SPC m g A open matching file in another window (e.g. switch between .cpp and .h)
SPC m D disaster: disassemble c/c++ code
SPC m r srefactor: refactor thing at point.

Note: semantic-refactor is only available for Emacs 24.4+