.emacs.d/init.d/programming/c++.el

135 lines
5.0 KiB
EmacsLisp

;;; c++.el --- C++ settings. -*- lexical-binding: t; -*-
;;; Commentary:
;;; Code:
(require 'basics/package-management)
(require 'basics/global-variables)
(require 'programming/common)
(when (and (executable-find "clang-tidy")
;; clang-tidy is built into clangd >= 9.
(< (my/clangd-version) 9.0))
(use-package flycheck-clang-tidy
:unless slow-computer
:after (flycheck projectile lsp-ui)
:defines (lsp-mode)
:functions (flycheck-clang-tidy-setup)
:config (progn
(defun my/clang-tidy-off ()
"Disable c/c++-clang-tidy."
(when (or (eq major-mode 'c++-mode)
(eq major-mode 'c-mode))
(add-to-list 'flycheck-disabled-checkers
'c/c++-clang-tidy)))
(defun my/clang-tidy-on ()
"Enable c/c++-clang-tidy."
(when (or (eq major-mode 'c++-mode)
(eq major-mode 'c-mode))
(setq-local flycheck-disabled-checkers
(remove 'c/c++-clang-tidy
flycheck-disabled-checkers))))
(defun my/flycheck-clang-tidy-setup ()
(flycheck-clang-tidy-setup)
;; Run clang-tidy after the lsp-ui checker.
(when lsp-mode
(unless (equal (flycheck-get-next-checker-for-buffer
'lsp-ui) 'c/c++-clang-tidy)
(flycheck-add-next-checker
'lsp-ui '(warning . c/c++-clang-tidy))))))
:hook ((flycheck-mode . my/flycheck-clang-tidy-setup)
(first-change . my/clang-tidy-off) ; Disable when file is modified.
(before-save . my/clang-tidy-on)))) ; Enable if file is saved.
;; Highlight Doxygen comments.
(use-package highlight-doxygen
:custom-face
(highlight-doxygen-comment ((t (:inherit font-lock-comment-face
:foreground "#667788"))))
(highlight-doxygen-code-block ((t (nil)))) ; Disable background-color.
:hook (prog-mode . highlight-doxygen-mode))
;; Set coding style.
(use-package cc-mode
:defer 2
:commands (cc-mode)
:config (progn
(c-add-style ; Construct own coding style definition.
"tastytea"
'("bsd"
(c-basic-offset . 4) ; Indent with 4 spaces.
(c-offsets-alist
. ((innamespace . [0]) ; Don't indent in namespaces.
(statement-cont . (c-lineup-assignments
c-lineup-string-cont
c-lineup-cascaded-calls
c-lineup-class-decl-init-after-brace
+))) )))
(c-add-style
"tastytea-legacy" ; For old code.
'("bsd"
(c-basic-offset . 4)))
(defun my/get-builddir ()
(if (member 'projectile-mode minor-mode-list)
(concat (projectile-project-root) "build")
nil)))
:custom (c-default-style "tastytea"))
;; Highlighting, indentation and documentation for CMake.
(use-package cmake-mode
:straight nil ; Only install if not installed by OS.
:init (unless (fboundp 'cmake-mode)
(straight-use-package
'(cmake-mode)))
:mode (("CMakeLists\\.txt$" . cmake-mode)
("\\.cmake$" . cmake-mode)))
;; Advanced highlighting for CMake.
(use-package cmake-font-lock
:after cmake-mode)
;; CMake reference.
(use-package eldoc-cmake
:after cmake-mode
:hook (cmake-mode . eldoc-cmake-enable))
(use-package gdb-mi
:straight (:type built-in)
:custom ((gdb-many-windows t)
(gdb-show-main t)))
;; ;; Extra highlighting.
;; (use-package modern-cpp-font-lock
;; :diminish modern-c++-font-lock-mode
;; :hook (c++-mode . modern-c++-font-lock-mode))
(use-package find-file
:defer 2
:custom (cc-search-directories '("." ; Set directories for ff-find-other-file.
"../src"
"../include"
"../../src"
"../../include"
"../src/*"
"../include/*"
"../../src/*"
"../../include/*"
"/usr/include"
"/usr/local/include/*")))
(when (executable-find "clang-format")
(use-package clang-format
:commands (clang-format-buffer my/clang-format-buffer-maybe)
:config (defun my/clang-format-buffer-maybe ()
"Run `clang-format-buffer' if `my/reformat-save' is t."
(when my/reformat-save
(clang-format-buffer)))
:hook ((c-mode-common
. (lambda () (add-hook 'before-save-hook
#'my/clang-format-buffer-maybe nil t))))))
(provide 'programming/c++)
;;; c++.el ends here