Giter Club home page Giter Club logo

emacs-clang-format-plus's Introduction

clang-format+ Build Status MELPA

Description

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.

Installation

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

Quelpa gives you an ability to install Emacs packages directly from remote git repos. I recommend using it with quelpa-use-package if you already use use-package.

Here how it's done:

(use-package clang-format+
  :quelpa (clang-format+
           :fetcher github
           :repo "SavchenkoValeriy/emacs-clang-format-plus"))

Install manually

First, install clang-format.el either from MELPA:

M-x package-install [RET] clang-format [RET]

or manually.

After that you should clone this repo and add the following code to you init.el file:

(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 (or _clang-format) file, or all C/C++ files if the variable clang-format-style is set to something else than "file". You can set clang-format+-always-enable to t to force formatting; then the default LLVM style will be used if not specified otherwise.

Project-level hook

If you don't want to enable formatting for all projects with a .clang-format/_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+))))

Customization

clang-format+ defines these variables that the user can tweak:

  • clang-format+-context defines 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-region defines the number of extra lines to reformat outside of a modified region both before and after (0 by default). If clang-format+-context is 'definition', the region will only be extended for modifications outside of definitions.
  • clang-format+-always-enable defines whether to enable formatting even if a style hasn't been selected. If clang-format+-always-enable is nil (which is the default), formatting will be enabled if there is a .clang-format/_clang-format file in the source tree or if clang-format-style is set to something else than "file". If non-nil, formatting will always be enabled.

Contribute

All contributions are most welcome!

It might include any help: bug reports, questions on how to use it, feature suggestions, and documentation updates.

Tributes

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.

License

GPL-3.0

emacs-clang-format-plus's People

Contributors

jrosdahl avatar savchenkovaleriy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

emacs-clang-format-plus's Issues

Only perform formatting when there is a .clang-format file?

Thanks for writing this package!

Earlier this summer I looked for an Emacs mode that runs clang-format automatically but couldn't find one, so I wrote my own: https://github.com/jrosdahl/auto-clang-format. The main, and only, feature of my package (except for defining a minor mode) is that it only runs clang-format-buffer when there is a .clang-format file in the directory tree. The reason is that I want to be able to activate the mode unconditionally in e.g. c-mode-common-hook but only have it take effect in projects that have defined a format style. Adding .dir-locals.el files to the different projects does not feel like a very tasty solution to me (and it might not even be possible).

I submitted my package for inclusion in MELPA (melpa/melpa#6383), but I then happened to find your package so I hope that it would be possible to collaborate on this instead of maintaining my own package (although I like my package name more since it enables auto-formatting ๐Ÿ™‚).

So, would you accept a pull request that adds the above mentioned behavior? If so, would you prefer it to be opt-in or opt-out? (Or if you want to implement it yourself, that would also be perfectly fine by me.)

Braces in comments cause errors

have stuff like this:

// type(M&& data) : data(::std::move(data)) {}
Debugger entered--Lisp error: (error "c-where-wrt-brace-construct: c-beginning-of-decl-1...")
  error("c-where-wrt-brace-construct: c-beginning-of-decl-1..." previous)
  c-where-wrt-brace-construct()
  c-beginning-of-defun()
  clang-format+-inside-of-enclosing-definition-p()
  clang-format+-get-region-internal(2205 c-beginning-of-defun previous-line)
  clang-format+-get-region-beginning(2205)
  #f(compiled-function (prop beg end) #<bytecode -0x13fc710a9140b881>)(chg 2205 2208)
  clang-format+-map-changes(#f(compiled-function (prop beg end) #<bytecode -0x13fc710a9140b881>))
  clang-format+-apply-to-modifications()
  clang-format+-before-save()
  run-hooks(before-save-hook)
  basic-save-buffer(t)
  save-buffer(1)
  funcall-interactively(save-buffer 1)
  command-execute(save-buffer)

The minor mode is not automatically activated

If I write in my init any one of these two configurations:

(use-package clang-format+
  :hook c-mode-common
  :requires clang-format)
(use-package clang-format+
  :config
  (add-hook 'c-mode-common-hook #'clang-format+-mode)
  :requires clang-format)

Solutions?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.