summaryrefslogtreecommitdiff
path: root/lisp
diff options
context:
space:
mode:
authormatthew <matt.kosarek@canonical.com>2025-12-08 20:20:46 -0500
committermatthew <matt.kosarek@canonical.com>2025-12-08 20:20:46 -0500
commita1e93569af7e744076a5253dbcdf3f4ed96196c2 (patch)
treeb6a391c43bd438c0821d9ba89203f522431979a0 /lisp
parent467abd17985cfdc1375ffbb1e192baa788bb1e40 (diff)
Add C++ auto format hook
Diffstat (limited to 'lisp')
-rw-r--r--lisp/cpp.el20
1 files changed, 19 insertions, 1 deletions
diff --git a/lisp/cpp.el b/lisp/cpp.el
index c22f771..d6b1cad 100644
--- a/lisp/cpp.el
+++ b/lisp/cpp.el
@@ -1,4 +1,9 @@
+(use-package clang-format
+ :ensure t)
+(use-package projectile
+ :ensure t)
+
;;; Code:
(defun setup-c()
"Set up c common mode."
@@ -26,10 +31,23 @@
(shell-command (format "cd %s && cmake -S . -B build -D CMAKE_EXPORT_COMPILE_COMMANDS=1" (projectile-project-root)))
)
-(defun maybe-regenerate-compile-commands()
+(defun mk/cpp/maybe-regenerate-compile-commands()
(if (string= (buffer-file-name) "CMakeLists.txt")
(mk/cpp/generate-compile-commands-cmake)))
+(defun mk/clang-format-buffer-if-project-file ()
+ "Run clang-format-buffer only if a .clang-format exists in the Projectile project."
+ (when (and (projectile-project-p) ; inside a projectile project
+ (locate-dominating-file (buffer-file-name) ".clang-format"))
+ (clang-format-buffer)))
+
+;; Add this hook to C/C++/ObjC modes
+(add-hook 'c-mode-common-hook
+ (lambda ()
+ (add-hook 'before-save-hook
+ #'mk/clang-format-buffer-if-project-file
+ nil 'local)))
+
(add-hook 'after-save-hook 'maybe-regenerate-compile-commands)
(provide 'cpp)