Skip to content

qxxt/clang-formatter.el

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 

Repository files navigation

clang-formatter.el

Description

clang-formatter is a function for formatting buffer using clang-format.

Installation

You can install by cloning this repo to your emacs directory. For example:

git clone https://github.com/qxxt/clang-formatter.el ~/.emacs.d/clang-formatter-el

And then adding that directory to your load-path:

(add-to-list 'load-path (expand-file-name "clang-formatter-el" user-emacs-directory))
(require ’clang-formatter)

Usage

You can simply call the function by M-x clang-format-buffer. You can bind that to a key in c-mode by:

(define-key c-mode-map (kbd "C-c C-b") 'clang-format-buffer)

To format before-save-hook on c-mode you can:

(add-hook 'c-mode-hook #'(lambda ()
                           (add-hook 'before-save-hook 'clang-format-buffer nil 'local)))

Or you can adds before-save-hook to all supported mode by:

(add-hook 'prog-mode-hook #'(lambda ()
                              (if (cl-find major-mode '(c-mode c++-mode java-mode javascript-mode objc-mode csharp-mode protobuf-mode))
                                  (add-hook ’before-save-hook 'clang-format-buffer nil 'local))))

Configuration

variabledescriptiondefault
clang-format-commandcommand for clang-format, can be absolute path to clang-format binary. (eg, “/usr/bin/clang-format-16”)“clang-format”
clang-format-stylestyle passed to clang-format“google”
clang-format-c-stylec-mode style for clang-formatnil
clang-format-c++-stylec++-mode style for clang-formatnil
clang-format-java-stylejava-mode style for clang-formatnil
clang-format-javascript-stylejavascript-mode style for clang-formatnil
clang-format-objc-styleobjc-mode style for clang-formatnil
clang-format-csharp-stylecsharp-mode style for clang-formatnil
clang-format-protobuf-styleprotobuf-mode style for clang-formatnil

You can customize style passed to clang-format by modifying clang-format-style. clang-format-style will be overriden by specific mode style if they are not nil.

Local Variable

You can configure styles from .dir-locals.el:

((c-mode . ((clang-format-c-style . "{BasedOnStyle: GNU,AlignAfterOpenBracket: Align,SortIncludes: Never}"))))

Macro

If you have clang-configuration file in root directory of your project, you can use “%R” and it will be replaced with your project root. For example, if project root is “~/project/abc”:

((nil . ((clang-format-style . "file:%R/.clang-format" ;; => --style=file:~/project/abc/.clang-format
                            ))))

About

Small function for formatting current buffer using clang-format.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published