Skip to content

Instantly share code, notes, and snippets.

@mlabbe
Last active June 17, 2019 20:57
Show Gist options
  • Save mlabbe/25d0a917516270d81d0a8b7817b0847a to your computer and use it in GitHub Desktop.
Save mlabbe/25d0a917516270d81d0a8b7817b0847a to your computer and use it in GitHub Desktop.
Run clang-format only when projects have a .clang-format in their root, while deferring the load of all packages with use-package
(use-package cc-mode
:defer t
:init
(add-hook 'before-save-hook #'clang-format-buffer-smart)
:config
(progn
(defun clang-format-buffer-smart()
"Reformat buffer only if .clang-format exists in the projectile root."
(when (file-exists-p (expand-file-name ".clang-format" (projectile-project-root)))
(clang-format-buffer)))
)
:bind (:map c-mode-base-map
("C-c u" . clang-format-buffer))
)
(use-package clang-format
:ensure t
:after cc-mode
:config
(setq clang-format-style-option "file")
:commands clang-format clang-format-buffer clang-format-region)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment