From aa97b3ac76e7c74c555cb64cf6353f1d32859f4a Mon Sep 17 00:00:00 2001 From: Joel Grunbaum <joelgrun@gmail.com> Date: Wed, 22 Dec 2021 07:39:02 +0000 Subject: [PATCH] Enable clang format with an attempt at automatic detection of a format config file --- config.org | 145 ++++++++++++++++++++++++++++++++++-------------- 1 files changed, 102 insertions(+), 43 deletions(-) diff --git a/config.org b/config.org index 9d8cd82..849d2c0 100644 --- a/config.org +++ b/config.org @@ -614,7 +614,7 @@ #+BEGIN_SRC emacs-lisp (setq c-default-style '((java-mode . "java") (awk-mode . "awk") - (other . "linux"))) + (other . "k&r"))) #+END_SRC ** Coding system @@ -624,12 +624,22 @@ (set-keyboard-coding-system 'utf-8) (set-language-environment "UTF-8") (prefer-coding-system 'utf-8) - (setq-default indent-tabs-mode t - tab-width 8 + (setq-default indent-tabs-mode nil + tab-width 4 c-basic-offset tab-width cperl-indent-level tab-width) + (c-set-offset 'inline-open '0) (delete-selection-mode) (global-set-key (kbd "RET") 'newline-and-indent) +#+END_SRC +*** Smart tabs +Tabs for indentation, spaces for alignment +#+BEGIN_SRC emacs-lisp + (use-package smart-tabs-mode + :ensure t + :config + (smart-tabs-insinuate 'c 'c++ 'java 'javascript 'cperl 'python 'ruby + 'nxml)) #+END_SRC ** Move to beginning of line ignoring whitespace @@ -692,7 +702,8 @@ ;; (setq tramp-default-method "ssh") (when (eq system-type 'windows-nt) (setq tramp-default-method "pscp")) - (setq password-cache-expiry nil)) + (setq password-cache-expiry nil) + (add-to-list 'tramp-remote-path 'tramp-own-remote-path)) #+END_SRC ** COMMENT Y or N instead of yes or no @@ -796,7 +807,6 @@ :config (flycheck-pos-tip-mode)) #+END_SRC - ** Company Company is auto-complete for Emacs. Uses various backends, more of which are added later. @@ -813,18 +823,19 @@ ** LSP Mode Use LSP for completion suggestions. Causes too much memory usage, need to debug. +Need to generate ~compile_flags~ for c/c++, can use ~bear~ but may need other tools. #+BEGIN_SRC emacs-lisp (use-package lsp-mode :ensure t :hook (((c-mode - cpp-mode + c++-mode tex-mode latex-mode TeX-mode LaTeX-mode rust-mode sh-mode - verilog-mode + ;; verilog-mode go-mode) . lsp)) :init (setq lsp-keymap-prefix "C-c l") @@ -835,11 +846,11 @@ (setq lsp-completion-provider :capf) (setq lsp-keep-workspace-alive 'nil) (add-to-list 'exec-path "~/.cargo/bin")) - + (use-package lsp-ui :ensure t :commands lsp-ui-mode) - + (use-package helm-lsp :ensure t :commands helm-lsp-workspace-symbol) @@ -960,7 +971,8 @@ ** Language specific configs *** C/C++ -**** Flycheck clang +**** Flycheck +***** Flycheck clang Add the clang backend for linting. #+BEGIN_SRC emacs-lisp (use-package flycheck-clang-analyzer @@ -971,8 +983,25 @@ (require 'flycheck-clang-analyzer) (flycheck-clang-analyzer-setup))) #+END_SRC +***** Flycheck project root +Flycheck tends to fail finding the project root, giving errors about missing files. +This should remove them. +#+BEGIN_SRC emacs-lisp + (defun setup-flycheck-project-path () + (let ((root (ignore-errors (projectile-project-root)))) + (when root + (add-to-list + (make-variable-buffer-local 'flycheck-clang-include-path) + root) + (add-to-list + (make-variable-buffer-local 'flycheck-gcc-include-path) + root)))) -**** Company + (add-hook 'c-mode-hook 'setup-flycheck-project-path) + (add-hook 'c++-mode-hook 'setup-flycheck-project-path) +#+END_SRC + +**** COMMENT Company Add header completion as well as Irony, which uses clang for suggestions. #+BEGIN_SRC emacs-lisp (use-package company-c-headers @@ -982,40 +1011,50 @@ (add-hook 'c++-mode-hook 'company-mode) (add-hook 'c-mode-hook 'company-mode)) - (use-package irony - :ensure t - :init - (setq w32-pipe-read-delay 0) - (setq irony-server-w32-pipe-buffer-size (* 64 1024)) - (add-hook 'c++-mode-hook 'irony-mode) - (add-hook 'c-mode-hook 'irony-mode) - (add-hook 'irony-mode-hook 'irony-cdb-autosetup-compile-options) - (add-hook 'irony-mode-hook 'irony-cdb-autosetup-compile-options)) + ;; (use-package irony + ;; :ensure t + ;; :init + ;; (setq w32-pipe-read-delay 0) + ;; (setq irony-server-w32-pipe-buffer-size (* 64 1024)) + ;; (add-hook 'c++-mode-hook 'irony-mode) + ;; (add-hook 'c-mode-hook 'irony-mode) + ;; (add-hook 'irony-mode-hook 'irony-cdb-autosetup-compile-options) + ;; (add-hook 'irony-mode-hook 'irony-cdb-autosetup-compile-options)) - (use-package company-irony - :ensure t - :after irony - :config - (add-to-list 'company-backends '(company-c-headers - company-dabbrev-code - company-irony))) + ;; (use-package company-irony + ;; :ensure t + ;; :after irony + ;; :config + ;; (add-to-list 'company-backends '(company-c-headers + ;; company-dabbrev-code + ;; company-irony))) #+END_SRC **** Clang-format Automatically format buffer on save. #+BEGIN_SRC emacs-lisp - (when (file-exists-p "/usr/share/clang/clang-format.el") - (load "/usr/share/clang/clang-format.el") - (setq clang-format-linux-style (concat "{BasedOnStyle: LLVM," - "IndentWidth: 8," - "UseTab: Always," - "BreakBeforeBraces: Linux," - "AllowShortIfStatementsOnASingleLine: false," - "IndentCaseLabels: false}")) - (setq-default clang-format-style clang-format-linux-style) - (defun clang-format-on-save () - (add-hook 'before-save-hook 'clang-format-buffer nil t)) - (add-hook 'c-mode-hook 'clang-format-on-save nil t) - (add-hook 'c++-mode-hook 'clang-format-on-save nil t)) + (defun set-clang-format-style () + (if (file-exists-p (concat + (projectile-project-root) + ".clang-format")) + (setq-local clang-format-style nil) + (setq-local clang-format-style (concat "{BasedOnStyle: LLVM," + "IndentWidth: " (format "%s" tab-width) "," + "UseTab: " (if (eq indent-tabs-mode nil) + "Never" + "AlignWithSpaces") + "," + "BreakBeforeBraces: Linux," + "AllowShortIfStatementsOnASingleLine: false," + "IndentCaseLabels: false}")))) + + (use-package clang-format + :ensure t + :hook (c-mode-common . (set-clang-format-style))) + + ;; (defun clang-format-on-save () + ;; (add-hook 'before-save-hook 'clang-format-buffer nil t)) + ;; (add-hook 'c-mode-hook 'clang-format-on-save nil t) + ;; (add-hook 'c++-mode-hook 'clang-format-on-save nil t)) #+END_SRC *** emacs-lisp **** COMMENT Company @@ -1168,9 +1207,29 @@ :pin gnu :config (autoload 'verilog-mode "verilog-mode" "Verilog mode" t ) - (add-to-list 'auto-mode-alist '("\\.[ds]?va?h?\\'" . verilog-mode))) + (add-to-list 'auto-mode-alist '("\\.[ds]?va?h?\\'" . verilog-mode)) + (setq-default verilog-align-ifelse t + verilog-auto-delete-trailing-whitespace t + verilog-auto-inst-param-value t + verilog-auto-lineup 'all + verilog-auto-newline nil + verilog-auto-save-policy nil + verilog-auto-template-warn-unused t + verilog-auto-endcomments nil + verilog-highlight-grouping-keywords t + verilog-highlight-modules t + verilog-tab-to-comment t + verilog-indent-begin-after-if nil + verilog-indent-lists nil + verilog-case-indent 4 + verilog-cexp-indent 0 + verilog-indent-level 4 + verilog-indent-level-behavioral 4 + verilog-indent-level-declaration 4 + verilog-indent-level-directive 4 + verilog-indent-level-module 4)) #+END_SRC - +#+END_SRC *** COMMENT MATLAB Mode for editing MATLAB m-files. #+BEGIN_SRC emacs-lisp @@ -1229,7 +1288,7 @@ (use-package flycheck-rust :ensure t :config - (add-hook 'flyckeck-mode-hook #'flycheck-rust-setup)) + (add-hook 'flycheck-mode-hook #'flycheck-rust-setup)) #+END_SRC *** Go **** Major mode -- Gitblit v1.9.3