From 37b803817c70a73c221d3f9126a06e9fc6eb4d10 Mon Sep 17 00:00:00 2001 From: Niclas Hofmann Date: Thu, 2 May 2024 00:12:48 +0200 Subject: [PATCH] emacs: improve c/c++ formatting --- .emacs.d/init.el | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/.emacs.d/init.el b/.emacs.d/init.el index 5e34ef8..7362d66 100644 --- a/.emacs.d/init.el +++ b/.emacs.d/init.el @@ -42,13 +42,28 @@ ;; "\\\\" "://")) ;; (global-ligature-mode t)) -;; C -(setq c-default-style "linux") - -(require 'eglot) -(add-to-list 'eglot-server-programs'((c++-mode c-mode) "clangd")) -(add-hook 'c-mode-hook 'eglot-ensure) -(add-hook 'c++-mode-hook 'eglot-ensure) +;; C/C++ +(defun my-c/cpp-mode-common-hook () + (c-set-offset 'substatement-open 0) + (setq c++-tab-always-indent t) + (setq c-basic-offset 4) + (setq c-indent-level 4) + (setq tab-stop-list '(4 8 12 16 20 24 28 32 36 40 44 48 52 56 60)) + (setq tab-width 4) + (setq indent-tabs-mode t)) +(add-hook 'c-mode-common-hook 'my-c/cpp-mode-common-hook) + +(use-package clang-format) +(defun clang-format-save-hook () + (add-hook 'before-save-hook + (lambda () + (when (locate-dominating-file "." ".clang-format") + (clang-format-buffer)) + nil) + nil + t)) +(add-hook 'c-mode-hook (lambda () (clang-format-save-hook))) +(add-hook 'c++-mode-hook (lambda () (clang-format-save-hook))) ;; Go (use-package go-mode @@ -74,8 +89,7 @@ (use-package yasnippet :commands yas-minor-mode - :hook '((c++-mode . yas-minor-mode) - (go-mode . yas-minor-mode) + :hook '((go-mode . yas-minor-mode) (zig-mode . yas-minor-mode))) ;; Lua