Skip to content

Commit

Permalink
emacs: improve c/c++ formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
niclas committed May 1, 2024
1 parent 7e34f55 commit 37b8038
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions .emacs.d/init.el
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 37b8038

Please sign in to comment.