clang-format+ is a small package aimed at improving the user experience of using clang-format in Emacs.
The existing package (clang-format.el) provides a wrapper around the CLI allowing its users to format buffers and regions. The workflow it suggests is a bit too manual, so custom
before-save-hooks and then
minor-modes come to play. clang-format+ joins all these customizations in order to remove all the duplicated ad-hocs.
clang-format+ defines a minor mode
clang-format+-mode, which applies clang-format on save. It can also apply clang-format to the modified parts of the region only and try to be smart about it.
Install from MELPA
You can install clang-format+ from MELPA by simply executing the following command:
M-x package-install [RET] clang-format+ [RET]
Install using Quelpa
Here how it's done:
(use-package clang-format+ :quelpa (clang-format+ :fetcher github :repo "SavchenkoValeriy/emacs-clang-format-plus"))
First, install clang-format.el either from MELPA:
M-x package-install [RET] clang-format [RET]
After that you should clone this repo and add the following code to you
(use-package clang-format+ :load-path "<path/to/my/cloned/clang-format+/directory>")
Setting up a hook
You can use clang-format+ for all C/C++ projects you edit:
(add-hook 'c-mode-common-hook #'clang-format+-mode)
This will enable automatic formatting of C/C++ files in source trees with a
_clang-format) file, or all C/C++ files if the variable
clang-format-style is set to something else than "file". You can set
t to force formatting; then the default LLVM style will be used if not specified otherwise.
If you don't want to enable formatting for all projects with a
_clang-format file, you can do it selectively by adding a .dir-locals.el file in the root directory of your project with the following code inside:
((c++-mode . ((mode . clang-format+))))
clang-format+ defines these variables that the user can tweak:
clang-format+-contextdefines how much context to reformat after modifications. Possible values:
'buffer: Reformat the whole buffer.
'definition: Reformat the enclosing definition (class/function/etc., but not namespace). This is the default.
'modification: Reformat only the modified parts.
clang-format+-offset-modified-regiondefines the number of extra lines to reformat outside of a modified region both before and after (
0by default). If
'definition', the region will only be extended for modifications outside of definitions.
clang-format+-always-enabledefines whether to enable formatting even if a style hasn't been selected. If
nil(which is the default), formatting will be enabled if there is a
_clang-formatfile in the source tree or if
clang-format-styleis set to something else than "file". If non-
nil, formatting will always be enabled.
All contributions are most welcome!
It might include any help: bug reports, questions on how to use it, feature suggestions, and documentation updates.
Many thanks to the authors of clang-format.
clang-format+ is pretty much a direct clone of the ws-butler package in the way it tracks changes, which in its turn copies this mechanism from highlight-changes-mode (probably we should make a mode that will be used as a base for all other modes). Please, check out those nice modes as well.